← All modules 04 From circuit diagrams to OpenQASM

SESAME · Day 2 · Module 04

From circuit diagrams
to OpenQASM

q₀ |0⟩ q₁ |0⟩ H result result

We need to describe this circuit in a form that quantum software can use.

How do we give the computer these instructions and check what comes back?

Common language / Measurement records / Simulation and statistics

OpenQASM describes the circuit; its measurement records need a statistical interpretation.

01 OpenQASM gives quantum software a common description of a circuit.

02 Measurement destinations and display order determine what each returned bit means.

03 Simulators calculate model predictions that we compare with statistics from repeated runs.

01 / Describing circuits · 1.1

OpenQASM gives different tools a common circuit description.

OpenQASM is an open language for describing and exchanging quantum circuits.

The circuit we want to describe

q₀ |0⟩ q₁ |0⟩ H result result
OPENQASM 2.0;include "qelib1.inc";

2.0: the language version used here.
Include: the definitions of gates such as h and cx.

Supporting tools can read the same circuit description and prepare it for execution.

01 / Describing circuits · 1.1

Qubits and classical bits have different jobs.

q: qubits, before the first gate

q₀ |0⟩ q₁ |0⟩

c: classical storage

c[0]0
c[1]0
OPENQASM 2.0;include "qelib1.inc";qreg q[2];creg c[2];

Two qubits and two classical bits.
Both registers start at zero in these programs.

Gates act on q. Measurement results are stored in c.

01 / Describing circuits · 1.2

Gate lines specify the qubits and operation order.

Operations run left to right

q₀ |0⟩ q₁ |0⟩ H

q[0]
control

q[1]
target

OPENQASM 2.0;include "qelib1.inc";qreg q[2];creg c[2];h q[0];cx q[0], q[1];

We read the instructions from top to bottom.
cx control, target;

The qubits and the sequence matter as much as the gate names.

01 / Describing circuits · 1.3

A measurement writes an outcome into a classical bit.

Measurement destinations

q₀ |0⟩ q₁ |0⟩ H c[0] c[1]

Each stored outcome is 0 or 1.

OPENQASM 2.0;include "qelib1.inc";qreg q[2];creg c[2];h q[0];cx q[0], q[1];measure q[0] -> c[0];measure q[1] -> c[1];

The destination after the arrow tells us where the outcome is stored.

01 / Describing circuits · 1.3

OpenQASM describes the gates and readout.

The same preparation and readout

q₀ |0⟩ q₁ |0⟩ H c[0] c[1]
OPENQASM 2.0;include "qelib1.inc";qreg q[2];creg c[2];h q[0];cx q[0], q[1];measure q -> c;
measure q -> c;→measure q[0] -> c[0];
measure q[1] -> c[1];

For these equal-size registers, the shorthand pairs matching indices.

02 / Running circuits and reading results · 2.1

Execution software runs the circuit and returns measurement records.

OpenQASM program

Our Bell preparation
and measurements.

Loaded and prepared by execution software.

How we run it

Simulator
A classical calculation of a model.
Quantum device
Operations on physical qubits.

Measurement records

c[1]?
c[0]?

We read these using the mapping and display order.

Qiskit is a Python toolkit that can load OpenQASM and arrange execution.

The execution setup chooses the simulator or device and the number of runs.

02 / Running circuits and reading results · 2.1

Here, c[1] is printed before c[0].

Stored classical bits

c[0]0 or 1
c[1]0 or 1

Positions in the displayed string

c[1]?
c[0]?

The rightmost digit is c[0]. This is a display convention.

Teaching ket: \(|q_0q_1\rangle\) · Displayed record: c[1]c[0]

02 / Running circuits and reading results · 2.2 / A known unequal pair

We can follow one known result into the display.

q₀ |0⟩ q₁ |0⟩ X c[0] c[1]

Qubit results

q₀ = 0
q₁ = 1

\[|q_0q_1\rangle=|01\rangle\]

Stored in c

c[0] = 0
c[1] = 1

Displayed record

c[1]1
c[0]0

Teaching ket: \(|q_0q_1\rangle\) · Displayed record: c[1]c[0]

02 / Running circuits and reading results · 2.2 / Prediction

Which string will we display now?

We keep the same X gate and change only the measurement destinations.

q₀ |0⟩ q₁ |0⟩ X c[1] c[0]
measure q[0] -> c[1];
measure q[1] -> c[0];

The record is 01.

q₀ = 0 is stored in c[1].
q₁ = 1 is stored in c[0].

c[1]0
c[0]1

Teaching ket: \(|q_0q_1\rangle\) · Displayed record: c[1]c[0]

02 / Running circuits and reading results · 2.3

Unequal test bits make the mapping visible.

Measured results
(q₀, q₁)
Matching destinations
q₀ → c[0], q₁ → c[1]
Exchanged destinations
q₀ → c[1], q₁ → c[0]
(0, 0)0000
(1, 1)1111
(0, 1)1001

The Bell circuit hides this difference. Unequal bits let us check the mapping.

Both output columns use c[1]c[0].

03 / Simulation and statistics · 3.1 / An ideal calculation

Statevector simulation updates the amplitudes.

A statevector is the list of complex amplitudes.
The simulator stores it and applies each gate’s linear transformation.

\[|00\rangle\xrightarrow{H\text{ on }q_0}\frac{|00\rangle+|10\rangle}{\sqrt2}\xrightarrow{\mathrm{CNOT}}\frac{|00\rangle+|11\rangle}{\sqrt2}\]
Before measurement\(|00\rangle\)\(|01\rangle\)\(|10\rangle\)\(|11\rangle\)
Calculated amplitude\(1/\sqrt2\)00\(1/\sqrt2\)
Predicted probability½00½

Squared magnitudes give model probabilities.
Sampling them produces simulated measurement records.

For n qubits, this method stores 2ⁿ amplitudes. Basis labels use \(|q_0q_1\rangle\).

03 / Simulation and statistics · 3.2

One shot produces one record.

A shot is one complete repetition of this experiment.

Preparation

\[|00\rangle\]

Gates

H on q₀
CNOT q₀ → q₁

Measurement

q₀ → c[0]
q₁ → c[1]

Record

c[1]?
c[0]?

Shot 1 11

Shot 2 00

Shot 3 11

Each physical shot starts with a fresh preparation.

Illustrative records from the same ideal Bell circuit.

03 / Simulation and statistics · 3.2

Relative frequencies estimate measurement probabilities.

100 shots / Illustrative data

Record c[1]c[0]Count
0046
1154

46 + 54 = 100 records.

Observed frequency of 11

\[\frac{54}{100}=0.54\]

This estimates \(P(11)\).

The ideal prediction is 0.5.

A count is a tally. Dividing by the shot total gives a relative frequency.

03 / Simulation and statistics · 3.3 / Prediction

Does 54 out of 100 mean the circuit is wrong?

Ideal prediction

½

Probability of record 11.

Expected count in 100 shots: 50.

Illustrative sample

54 / 100

Observed frequency: 0.54.

Same circuit and readout.

No. A finite sample can differ from the predicted proportions.

More shots generally improve the estimate. A particular sample need not split exactly in half.

03 / Simulation and statistics · 3.3 / A different report

Can sampling alone explain this 01 record?

The same ideal Bell circuit

\[P(00)=P(11)=\tfrac12\]
\[P(01)=P(10)=0\]

Matching measurement destinations.
Perfect gates and readout.

100 shots / Illustrative report

Record c[1]c[0]Count
0046
011
1153

No. Sampling cannot produce an outcome with zero ideal probability.

We need to check the circuit, the recording, the execution model and the reported data.

Common language / Measurement records / Simulation and statistics

OpenQASM describes the circuit; its measurement records need a statistical interpretation.

01 OpenQASM gives quantum software a common description of a circuit.

02 Measurement destinations and display order determine what each returned bit means.

03 Simulators calculate model predictions that we compare with statistics from repeated runs.

Exercise / SESAME Module 04

From the diagram to a program

04.01 · Build the OpenQASM program ↗
01 / REGISTERS

Which qubits and classical bits does the program need?

02 / OPERATIONS

Which gates, operands and order match the diagram?

03 / RECORDING

Where does each measurement result go?

OpenQASM makes the circuit’s operations and readout explicit.