Learning Home Catalog Composer
Learning
Home Catalog Composer
Tutorials

Repeat until success

Estimated QPU usage: 4 seconds (tested on IBM Canberra)

Background

This tutorial demonstrates how certain IBM Quantum™ systems (those that support dynamic circuits) use mid-circuit measurements to produce a circuit that repeatedly attempts its setup until a syndrome measurement reveals that it has been successful.

Build an abstract circuit that uses the non-parametrized gate set {H,X,S,Toffoli}\{H,\,X,\,S,\,\text{Toffoli}\} to construct a heralded RX(θ)R_X(\theta) gate on a target qubit, where θ\theta satisfies cosθ=35\cos\theta = \frac35. Each iteration of the circuit has a finite chance of success, but because successes are heralded, dynamic circuit capabilities are used to repeat the setup until it succeeds.

Requirements

Before starting this tutorial, ensure that you have the following installed:

  • Qiskit SDK v1.0 or later, with visualization support ( pip install 'qiskit[visualization]' )
  • Qiskit Runtime 0.22 or later (pip install qiskit-ibm-runtime)

Step 1: Map classical inputs to a quantum problem

Copy to clipboard

No output produced

A single trial of the probabalistic gate has the following form:

Copy to clipboard

No output produced

Copy to clipboard

Output:

If both control bit measurements return 00, the gate RX(θ)R_X(\theta) with cosθ=35\cos\theta = \frac35 is applied. If any of the measurements are 11, gate XX is applied, which is a failure. This is the heralding; you can tell from the measurement whether the correct gate was applied, without disturbing the coherence of the target qubit. Without fully reproducing the mathematics, the success probability of this gate is:

Psuccess=3+i23+i2+31i2=58P_{\text{success}} = \frac{ {|3 + i|}^2 } { {|3 + i|}^2 + 3{|1 - i|}^2 } = \frac58

If there is a failure, reset the "dirty" state and start again. Because you know what is applied in the case of a failure, you can use this knowledge to perform the reset efficiently without using a general hardware reset. For the two auxiliary qubits, this is an XX gate conditioned on its respective measurement being 11. IBM® hardware has a special fast path for when qubits are conditioned on the result of their own measurement, so this is more efficient than most control flows.

Copy to clipboard

No output produced

Qiskit cannot directly represent an inequality condition, which is required for this exercise. You only need to repeat if the mid-circuit measurement result was not the bitstring "00". Instead, you can create an if statement with the condition mid_measure == "00", pass an empty block, and then use the else branch to perform the necessary logic.

Copy to clipboard

Output:

Step 2. Optimize problem for quantum execution

To reduce the total job execution time, V2 primitives only accept circuits and observables that conforms to the instructions and connectivity supported by the target system (referred to as instruction set architecture (ISA) circuits and observables).

Convert to ISA input

Copy to clipboard

Output:

>>> Connected to ibm_cairo backend.

Copy to clipboard

Output:

Step 3. Execute using Qiskit primitives

Copy to clipboard

Output:

'cs29tes965y000852sb0'
Copy to clipboard

Output:

>>> Hardware counts for mid: {'10': 480, '00': 2852, '11': 313, '01': 451}
>>> Hardware counts for final: {'1': 1590, '0': 2506}

Copy to clipboard

Output:

>>> Hardware merged data: {'10-1': 224, '00-1': 971, '10-0': 256, '11-1': 198, '00-0': 1881, '11-0': 115, '01-0': 254, '01-1': 197}

Step 4. Post-process, return result in classical format

A successful result is one in which the measurements on the two controls end in the 00 state. You can filter those out and see how many successes will result. This is a type of post-selection. In the complete repeat-until-success circuit with a dynamic while loop, this would not be necessary, as you would be guaranteed a success. However, in the interim, you can use the probabilities to examine the quality of the output and verify that the probabilities are as expected.

Copy to clipboard

No output produced

Copy to clipboard

Output:

Expected success rate 0.859.  Actual rate 0.696.

We can also test the post-selected output state:

Copy to clipboard

Output:

Expected |1> probability: 0.2.  Actual probability 0.340.

Tutorial Survey

SurveyTutorial Survey
Please take one minute to provide feedback on this tutorial. Your insights will help us improve our content offerings and user experience.

Was this page helpful?