QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
HomeLearnCatalogRanksPlaygroundDocs
02 · if / else on a measured bit0%

You are reading without signing in. Sign in or continue as guest to take quizzes and climb the ranks.

Learn/QC 201Dynamic Circuits·Lesson 2 of 4

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.

Intermediate12 min read4 sections · 2 questions+45
By the end you will be able to
  1. 01Insert an if/else frame from the palette and fill it.
  2. 02Write conditions on bits, registers and combinations (&&, ||, ^).
  3. 03Translate `with qc.if_test((c[0], 1)):` by hand.
Python
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)
The classical bit c[0] decides which gate runs on q1. In the composer the if and else frames carry the condition on a tag under the classical wire.
Sign in to run (nothing is saved)
Open the Python in the IDE Run in the Playground
ConditionTrue 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)
Hands-on

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
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];
Sign in to run (nothing is saved)
Open in the IDE Open in the Playground

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.

Knowledge check

2 questions · one try each counts

Question 1

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 climb the ranks; you will come back to this lesson.

Question 2

What does `with qc.if_test((c[0], 1)) as else_:` followed by `with else_:` produce in QASM?

Sign in to answer and climb the ranks; you will come back to this lesson.

Key takeaway

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.

Finished reading?

Knowledge check: 2 questions waiting above. Questions are optional. Mark it complete, or press Next: moving on marks it complete too.

Up next
Lesson 3 · 12 min
Loops and switch
Repeat until success with while, unroll constant for loops, pick a branch with switch.
Further reading
  • Docs: condition forms
More in Dynamic Circuits
  • Measuring in the middle
  • Teleportation with feed-forward
Measuring in the middleLoops and switch
In this lesson0%
  1. Introduction
  2. Try it: Build it yourself
  3. Knowledge check
  4. Key takeaway
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in