You are reading without signing in. Sign in or continue as guest to take quizzes and keep XP.
if / else on a measured bit
Feed-forward: apply a correction only when a bit says so. Build it from the palette and write it in both languages.
- Insert an if/else frame from the palette and fill it.
- Write conditions on bits, registers and combinations (&&, ||, ^).
- Translate `with qc.if_test((c[0], 1)):` by hand.
from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.measure(0, 0)
with qc.if_test((c[0], 1)) as else_:
qc.x(1)
with else_:
qc.z(1)
qc.measure(1, 1)OPENQASM 3.0;
include "stdgates.inc";
qubit[2] q;
bit[2] c;
h q[0];
c[0] = measure q[0];
if (c[0] == true) {
x q[1];
} else {
z q[1];
}
c[1] = measure q[1];
| Condition | True when |
|---|---|
| if (c[0]) | bit 0 is 1 |
| if (!c[0]) | bit 0 is 0 |
| if (c == 3) | the register reads binary 11 |
| if (c[0] && c[1]) | both bits are 1 |
| if ((c[0] ^ c[1]) == true) | exactly one bit is 1 (odd parity) |
Build it yourself
Start from this measured Bell pair. Palette → Control flow → if / else, tap column 3; drag an X into the if frame on q1; type the condition c[0] == true in the inspector. Simulate: q1 is now always 0.
OPENQASM 3.0; include "stdgates.inc"; qubit[2] q; bit[2] c; h q[0]; cx q[0], q[1]; c[0] = measure q[0]; c[1] = measure q[1];
Qiskit only imports the simplest conditions. QubitLab lowers &&, ||, ^, !, != and register comparisons into nested single-bit tests before Qiskit sees them, so you may write the natural form in either language.
In OpenQASM 3, which condition is true when the two-bit register c holds 10 (c[1] = 1, c[0] = 0)?
Sign in to answer and earn XP; you will come back to this lesson.
What does `with qc.if_test((c[0], 1)) as else_:` followed by `with else_:` produce in QASM?
Sign in to answer and earn XP; you will come back to this lesson.
if/else lets a measured bit steer later gates. QubitLab draws it as frames, accepts rich conditions, and writes both the QASM and the Qiskit form.
Up next: Loops and switch