Learning Home Catalog Composer Lab
Learning
Home Catalog Composer Lab Return to tutorial
Learning
Repeat until success
BackgroundSetupBuild problemRun in local testing modeConvert to ISA inputRun the jobRun on IBM hardwareConvert to ISA inputRun the jobAnalyze the results

Repeat until success

Category

How-to

Topics

Dynamic circuits
Download notebook Download notebook

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.

Setup

Authenticate to run code cells
Reset Copy to clipboard

No output produced

Build problem

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

Authenticate to run code cells
Reset Copy to clipboard

No output produced

Authenticate to run code cells
Reset 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.

Authenticate to run code cells
Reset 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.

Authenticate to run code cells
Reset Copy to clipboard

Output:

Run in local testing mode

Authenticate to run code cells
Reset Copy to clipboard

No output produced

Convert to ISA input

Authenticate to run code cells
Reset Copy to clipboard

Output:

Run the job

Authenticate to run code cells
Reset Copy to clipboard

Output:

>>> Simulator counts for mid: {'00': 804, '10': 71, '01': 63, '11': 86}
>>> Simulator counts for final: {'0': 696, '1': 328}

Run on IBM hardware

Authenticate to run code cells
Reset Copy to clipboard

Output:

>>> Connected to ibm_kyiv backend.

Convert to ISA input

Authenticate to run code cells
Reset Copy to clipboard

Output:

Run the job

Authenticate to run code cells
Reset Copy to clipboard

Output:

'crh9kwzx30tg008kmm4g'
Authenticate to run code cells
Reset Copy to clipboard

Output:

>>> Hardware counts for mid: {'00': 3165, '01': 322, '10': 296, '11': 313}
>>> Hardware counts for final: {'1': 1505, '0': 2591}

Authenticate to run code cells
Reset Copy to clipboard

Output:

>>> Hardware merged data: {'00-1': 993, '01-0': 147, '00-0': 2172, '10-0': 127, '10-1': 169, '11-0': 145, '11-1': 168, '01-1': 175}

Analyze the results

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.

Authenticate to run code cells
Reset Copy to clipboard

No output produced

Authenticate to run code cells
Reset Copy to clipboard

Output:

Expected success rate 0.859.  Actual rate 0.773.

We can also test the post-selected output state:

Authenticate to run code cells
Reset Copy to clipboard

Output:

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

Authenticate to run code cells
Reset Copy to clipboard

Output:

'0.22.0'
Authenticate to run code cells
Reset Copy to clipboard

Output:

'1.0.2'

Was this page helpful?

YesNo