SESAME · Day 2 · Module 04
From circuit diagrams
to OpenQASM
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
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
c: classical storage
c[0]0c[1]0OPENQASM 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]
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
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
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
A classical calculation of a model.
Operations on physical qubits.
Measurement records
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 1c[1]0 or 1Positions in the displayed string
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.
Qubit results
q₀ = 0
q₁ = 1
Stored in c
c[0] = 0c[1] = 1
Displayed record
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.
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].
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) | 00 | 00 |
| (1, 1) | 11 | 11 |
| (0, 1) | 10 | 01 |
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.
| Before measurement | \(|00\rangle\) | \(|01\rangle\) | \(|10\rangle\) | \(|11\rangle\) |
|---|---|---|---|---|
| Calculated amplitude | \(1/\sqrt2\) | 0 | 0 | \(1/\sqrt2\) |
| Predicted probability | ½ | 0 | 0 | ½ |
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
Gates
H on q₀
CNOT q₀ → q₁
Measurement
q₀ → c[0]
q₁ → c[1]
Record
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 |
|---|---|
00 | 46 |
11 | 54 |
46 + 54 = 100 records.
Observed frequency of 11
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
Matching measurement destinations.
Perfect gates and readout.
100 shots / Illustrative report
| Record c[1]c[0] | Count |
|---|---|
00 | 46 |
01 | 1 |
11 | 53 |
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 ↗Which qubits and classical bits does the program need?
Which gates, operands and order match the diagram?
Where does each measurement result go?
OpenQASM makes the circuit’s operations and readout explicit.