T-State Injection and a Heralded Repeat-Until-Success Loop#
This tutorial prepares the magic state |T> on one transmon, measures how
good it is, and makes it better with a herald: a readout, a decision on the
controller and a correcting pulse, repeated until the readout says the
qubit starts where it should. Then it injects the state into a three-qubit
repetition code with two CNOTs made of cross-resonance pulses.
The herald takes the infidelity of the physical state from about 3e-2 to 2e-5 as an ideal readout measures it, and to 2e-3 as the chip’s readout model measures it; 1.5e-3 of that is the error of the model’s readouts in the tomography itself. The encoded state comes out with a fidelity of 0.98.
What you’ll learn:
why the T gate costs no pulse on a transmon;
how to measure a state with tomography, and what the readout adds;
how to repeat a herald on the controller until it succeeds;
how to make a CNOT from a cross-resonance pulse and encode with it;
where the time and the error go.
Every number here is simulated, in a few seconds on a laptop CPU: the physical state on the 17-qubit transmon model, the injection on a chain of three coupled transmons that this page describes.
The page builds on Measure and React and Calling Classical Code During Quantum Runtime.
All the code on this page is also one Python script,
t_state_injection.py.
Why T states#
A fault-tolerant quantum computer gets its Clifford gates (H, S, CNOT) cheaply from its error-correcting code. It also needs one non-Clifford gate, usually \(T = \mathrm{diag}(1, e^{i\pi/4})\).
It makes T gates by consuming magic states:
The state is prepared on a physical qubit, injected into the code, and then purified:
by distillation (Bravyi and Kitaev, PRA 71, 022316 (2005); Litinski, Quantum 3, 205 (2019));
or by cultivation (Gidney, Shutty and Jones, arXiv:2409.17595 (2024)).
The cost of those steps grows quickly with the error of the starting state,
so a better physical |T> makes everything after it cheaper.
The T gate is a phase change of the drive tone#
On a transmon, a Z rotation needs no pulse. Shifting the phase of the drive tone turns every later pulse about a rotated axis, which is the same as rotating the qubit about Z.
CUDA-Q Pulse gives the phase gates Z, S and T as these virtual Z rotations. This tutorial uses D5, the middle data qubit of the 17-qubit model:
import math
import numpy as np
import cudaq_pulse as pulse
system = pulse.quantum_system("transmon_surface17")
qubit = system.qudit("D5")
t_gate = qubit.get_calibration(pulse.gates.T)
print(t_gate)
Output
VirtualZ(phase_rad=0.7853981633974483)
Preparing |T> takes a π/2 pulse about Y, which turns the ground state
into |+>, and then the T gate. List what the schedule plays:
@pulse.kernel
def prepare_t(qudit):
drive_line = get_drive_line(qudit)
y90 = drive_line.get_calibration(pulse.gates.Y90)
t_gate = drive_line.get_calibration(pulse.gates.T)
# |0> to |+>, then |+> to |T>.
drive(drive_line, y90)
drive(drive_line, t_gate)
print(f"{'kind':8} {'start':>6} {'length':>6}")
for event in prepare_t.lower(qubit).schedule:
start = event.start_cycles
length = event.duration_cycles
print(f"{event.kind:8} {start:>6} {length:>6}")
Output
kind start length
drive 0 80
The schedule holds one pulse, the 40 ns Y90. The T gate played no pulse: it advanced the phase of the drive line’s tone by π/4.
The schedule of prepare_t. The diamond is the T gate.#
Schedule of the prepare_t kernel on D5's drive line: a Y90 pulse from 0 to 40 ns, then the T gate as a +π/4 phase shift of the tone at 40 ns, a point with no length and no pulse.
What to notice: the T gate takes no time and adds no error. What limits
this |T> is the Y90 pulse, the qubit’s decoherence, and the state it
starts in.
Reading the state against the tone#
The tone’s phase lives on the control side. It changes what later pulses do, never the qubit’s state itself.
So the state that pulse.simulate returns is the same with and without
the T gate. Compare it with a kernel that stops at |+>, simulating both
from |0> in the three levels simulated per transmon:
@pulse.kernel
def plus(qudit):
drive_line = get_drive_line(qudit)
y90 = drive_line.get_calibration(pulse.gates.Y90)
drive(drive_line, y90)
ground = [1.0, 0.0, 0.0]
with_t = pulse.simulate(prepare_t, qubit, initial_state=ground)
without_t = pulse.simulate(plus, qubit, initial_state=ground)
same = np.allclose(with_t.density_matrix, without_t.density_matrix)
print("same state with and without T:", same)
Output
same state with and without T: True
Tomography#
A readout right after the T gate can’t see it either, since a Z rotation doesn’t change the populations a readout measures. What does see it is the next pulse.
State tomography uses that: a pulse turns the X, Y or Z axis onto Z, and a
readout measures it. This kernel prepares |T> and measures one axis.
After the state, one pulse picks the axis:
a -90 degree Y rotation turns +X onto +Z;
a +90 degree X rotation turns +Y onto +Z;
Z needs no pulse.
X_AXIS, Y_AXIS, Z_AXIS = 0, 1, 2
@pulse.kernel
def t_tomography(qudit, axis: int):
drive_line = get_drive_line(qudit)
readout_line = get_readout_line(qudit)
y90 = drive_line.get_calibration(pulse.gates.Y90)
t_gate = drive_line.get_calibration(pulse.gates.T)
x90 = drive_line.get_calibration(pulse.gates.X90)
measurement = readout_line.get_calibration(pulse.gates.READOUT)
drive(drive_line, y90)
drive(drive_line, t_gate)
if axis == X_AXIS:
drive(drive_line, y90.replace(amplitude=-y90.amplitude))
elif axis == Y_AXIS:
drive(drive_line, x90)
sync(drive_line, readout_line)
readout(readout_line, measurement)
The three schedules of t_tomography, one axis at a time.#
One pulse picks the axis. X axis: −Y90 turns +X onto Z. The readout gives ⟨X⟩ = 0.71. Y axis: X90 turns +Y onto Z. The readout gives ⟨Y⟩ = 0.71. Z axis: no pulse. The T gate shows as a +π/4 phase shift of the tone. The readout gives ⟨Z⟩ = 0.01.
What to notice: only the second pulse changes. Each one moves a different axis of the state onto Z, where the readout can see it.
From readouts to fidelity#
Each simulation is exact: its branches hold every outcome of the readout
with its probability. An axis reads ⟨Z⟩ = 1 - 2 P(nonzero). The three axes
make the Bloch vector, and its overlap with |T>’s gives the fidelity:
def last_readout(branch):
"""The level the last readout a shot took reported."""
return [level for level in branch.levels if level >= 0][-1]
def expectation(result):
"""<Z> of the last readout, from its outcomes' probabilities."""
excited = 0.0
for branch in result.branches:
if last_readout(branch) > 0:
excited += branch.probability
return 1 - 2 * excited
T_BLOCH = np.array([1.0, 1.0, 0.0]) / math.sqrt(2)
def fidelity(bloch):
"""<T|rho|T> for the Bloch vector, at most length 1."""
length = max(1.0, float(np.linalg.norm(bloch)))
unit = bloch / length
return (1 + float(unit @ T_BLOCH)) / 2
A branch lists a level for each readout of the program, and -1 for a
readout its shot didn’t reach. This kernel has one readout; the kernels
further down have readouts in the arms of an if.
A simulation reads out ideally unless told otherwise: every outcome is the
level the qubit is in. readout="model" reports each outcome through the
qubit’s calibrated assignment error, as the chip’s readout would. Measure
all three axes from |0>, both ways:
def measured_fidelity(mode, **start):
bloch = np.zeros(3)
for axis in (X_AXIS, Y_AXIS, Z_AXIS):
result = pulse.simulate(
t_tomography,
qubit,
axis,
readout=mode,
**start,
)
bloch[axis] = expectation(result)
return fidelity(bloch)
print("readout infidelity")
for mode in ("ideal", "model"):
measured = measured_fidelity(mode, initial_state=ground)
print(f"{mode:7} {1 - measured:.1e}")
Output
readout infidelity
ideal 2.4e-05
model 1.5e-03
What to notice:
with the ideal readout, the prepared state is within 3e-5 of
|T>, and that includes the tomography pulses’ own errors;the same state measured through the readout model is 1.5e-3 away. The state didn’t change. The difference is the error of the three readouts that measure it, and no tomography through this readout can report less.
The measured Bloch vector, seen from above, with the ideal readout.#
The Bloch vector the tomography measures from the ground state, seen from above, in the X-Y plane: x 0.71, y 0.71, z 0.01. It points 45 degrees from plus X, where the plus state would point, and reaches the unit circle.
What to notice: the state points 45° from |+>. That is the π/4 the
tone’s phase shift added, even though no pulse played it.
The thermal start#
A real transmon doesn’t start in |0>. It sits in a thermal mixture,
and the model starts D5 with part of its population already excited.
Leave out initial_state and the simulation starts there:
excited = system.thermal_populations[qubit.index]
print(f"excited at the start: {excited:.3f}")
print("readout infidelity")
for mode in ("ideal", "model"):
print(f"{mode:7} {1 - measured_fidelity(mode):.1e}")
Output
excited at the start: 0.027
readout infidelity
ideal 2.6e-02
model 2.9e-02
The levels D5 starts in, as the first herald readout of this page reads them with the ideal readout.#
The levels D5 starts in, as the first readout of the heralded kernel reads them: level 0, 97.2%; level 1, 2.7%; level 2, 7.5e-4. Everything not in level 0 is the error a herald removes.
What to notice: almost 3% of shots start outside level 0, nearly all of them in level 1.
The excited part of the start ends up near the opposite state. So the infidelity is about the thermal population: 2.6e-2, about a thousand times the error of the pulses, and far more than the readout’s.
That is the error to remove.
A herald on the controller#
A herald measures the qubit before the state is prepared, and goes on only if it read 0.
Here the decision runs on the controller, as a classical function the kernel calls while it runs. Its link is the controller’s own logic, with a few nanoseconds of fixed and feedback cost:
from cudaq_pulse.classical import Link
controller_link = Link(fixed_ns=4, feedback_ns=8, slots=2)
links = {"controller": controller_link}
control_system = pulse.ControlSystem(links=links)
The herald itself is one comparison, with a budget of tens of nanoseconds. The code from here on comes from examples/28_t_state_injection.py:
@pulse.classical(
min_latency_ns=20,
nominal_latency_ns=30,
max_latency_ns=40,
link="controller",
)
def herald(outcome: pulse.Bits) -> bool:
"""Accept the shot when the qubit read 0."""
return outcome[0] == 0
Repeat until success#
An attempt measures the qubit and submits the outcome. If the answer is
yes, the kernel prepares |T> and measures one axis, as t_tomography
does, and the shot is done. If it is no, a π pulse returns the qubit to
|0> and the kernel calls itself for the next attempt:
@pulse.kernel
def heralded_t(qudit, attempts: int, axis: int):
drive_line = get_drive_line(qudit)
readout_line = get_readout_line(qudit)
flip = drive_line.get_calibration(pulse.gates.X)
measurement = readout_line.get_calibration(pulse.gates.READOUT)
outcome = readout(readout_line, measurement)
request = herald.submit([outcome])
accepted = request.result()
sync(drive_line, readout_line)
if accepted:
# Success: prepare |T> and measure one axis. No more attempts.
t_tomography(qudit, axis)
else:
# Back to |0>, and try again while attempts remain.
drive(drive_line, flip)
sync(drive_line, readout_line)
if attempts > 1:
heralded_t(qudit, attempts - 1, axis)
What to notice:
a shot stops at its first success: the next attempt is in the arm of the
ifthat only a rejected shot takes;attemptsbounds the repeats. A kernel has nowhileloop and nobreakon a measured value, so the loop is written as the kernel calling itself, and the compiler lays out every attempt it could need.
Compile it against the control system with the controller link:
compiled = pulse.compile(
heralded_t,
quantum_system=system,
control_system=control_system,
)
heralded_t with at most two attempts, played for each way a shot can
go. Dashed pulses play only on the shots that take their arm.#
A herald repeated until it succeeds, then the T state. Attempt 1 reads D5 out. The outcome goes to herald on the controller link. herald accepts the shot. Y90 and the T gate's +π/4 phase shift of the tone prepare |T⟩. The last readout measures Z. If herald accepts attempt 1, no π pulse plays (97.2% of shots). If herald rejects attempt 1 and accepts attempt 2, π 1 plays (2.7% of shots). If herald rejects both attempts, both π pulses play (8.2e-4 of shots).
What to notice: the two arms of an attempt end together, so the program
has one length whichever way a shot goes. A shot that succeeds at once
plays its |T> pulses at the end of the program, when a second attempt
would play them, and spends the time before them in |0>.
Keep the shots that succeeded#
Each branch of a simulation records the classical calls it made. A shot
succeeded if its last herald answer was yes. postselect keeps the
branches a test accepts:
def succeeded(branch):
return branch.answers("herald")[-1]
The branches of one attempt, measured on Z, one readout at a time.#
What post-selection keeps. Every shot starts in D5's thermal mixture. The herald readout splits the shots: it reads 0 on 97.2%, 1 on 2.7%, 2 on 7.5e-4. Post-selection keeps the shots that succeeded, the 97.2% that read 0. The rest are dropped. The kept shots split about equally: Z reads 0 on 49.1% of all shots and 1 or 2 on 48.1%, as |T⟩ gives.
What to notice: with one attempt, the shots that succeeded are those whose
herald read 0. Among them, Z reads 0 and 1 about equally, as |T>
should.
The kept result holds their state, normalized, and acceptance, the
share of shots they are. Measure the fidelity of the shots that succeeded,
their share, and how many attempts a shot took on average:
def heralded_fidelity(attempts, mode="ideal"):
bloch = np.zeros(3)
for axis in (X_AXIS, Y_AXIS, Z_AXIS):
program = compiled.lower(qubit, attempts, axis)
result = pulse.simulate(program, readout=mode)
kept = result.postselect(succeeded)
bloch[axis] = expectation(kept)
# The attempts a shot took: its herald answers, over all shots.
taken = 0.0
for branch in result.branches:
taken += branch.probability * len(branch.answers("herald"))
return fidelity(bloch), kept.acceptance, taken
Try it with one attempt allowed, then two and three. Through the readout model a simulation follows every way a readout can misreport, so stop that one at two:
print("readout at most succeeded infidelity attempts")
for mode, most in (("ideal", 3), ("model", 2)):
for attempts in range(1, most + 1):
heralded, share, taken = heralded_fidelity(attempts, mode)
print(
f"{mode:7} {attempts:7} {share:9.4f} "
f"{1 - heralded:10.1e} {taken:8.3f}"
)
Output
readout at most succeeded infidelity attempts
ideal 1 0.9723 2.4e-05 1.000
ideal 2 0.9992 2.4e-05 1.028
ideal 3 0.9993 2.4e-05 1.029
model 1 0.9696 1.6e-03 1.000
model 2 0.9964 2.0e-03 1.030
The error of each start, from this page’s tomography with the ideal readout.#
Infidelity of the prepared state with the T state, measured with the ideal readout: start in |0⟩, 2.4e-05; thermal start, 2.6e-02; thermal, 1 herald, 2.4e-05. One herald brings the thermal start back to the error of the ground-state start.
The shots that succeeded, by the attempts allowed, with the ideal readout.#
The share of shots whose herald succeeds, by the attempts allowed, with the ideal readout: 1, 97.2%; 2, 99.9%; 3, 99.9%. The second attempt recovers most of the shots the first rejected; a third adds almost nothing.
What to notice, with the ideal readout:
one attempt succeeds on the 97.2% of shots that didn’t start excited, and their infidelity falls from 2.6e-2 to 2.4e-5, the result from
|0>;a second attempt recovers almost all the rest, at the same fidelity, and a shot takes 1.03 attempts on average;
a third gains almost nothing: about 7e-4 of the shots never succeed.
Those start in level 2, about 8e-4 of the thermal start from the model’s Boltzmann ratio. They read nonzero on every attempt, because the loop’s π pulse drives only the 0-1 transition. A π pulse at the 1-2 tone after a readout of 2 would bring them back.
And with the readout model:
the herald still removes the thermal start: the infidelity falls from 2.9e-2 to 1.6e-3, next to the 1.5e-3 that the tomography’s own readouts cost from
|0>;a second attempt adds a few 1e-4, to 2.0e-3: the shots it adds are worse than the ones that succeed at once;
a misread herald also rejects good shots, so two attempts succeed on 99.6% of shots, against 99.9% with the ideal readout.
When the answers are due#
Every attempt waits for its answer before its pulse can play, and a qubit that waits decoheres. The compiler places each call and reports when its answer is due, here in nanoseconds:
program = compiled.lower(qubit, 1, Z_AXIS)
print("function sent (ns) due (ns) slack")
for call in program.classical_report():
sent_ns = call.submit_cycles / system.clock_ghz
due_ns = call.due_cycles / system.clock_ghz
slack = call.slack_cycles
print(f"{call.function:8} {sent_ns:9} {due_ns:8} {slack:5}")
Output
function sent (ns) due (ns) slack
herald 300.0 352.0 0
The outcome is sent when the 300 ns readout ends. The answer is due 52 ns later:
4 ns on the link;
at most 40 ns in the function;
8 ns of feedback.
The first answer of heralded_t, close up.#
A close look at the first herald answer. The readout ends and its outcome is sent to the herald function, whose answer is due 52 ns later: 4 ns on the link, at most 40 ns in the function and 8 ns of feedback. The pi pulse that plays only if the qubit read 1 starts the moment the answer is due.
What to notice: the π pulse starts the moment the answer is due. That is why the slack is 0.
A herald whose answer comes late, or a decoder that answers in microseconds instead of nanoseconds, moves every later attempt by as much.
A chain to inject into#
A physical |T> is the start. To be used, it is injected into a code:
entangled with other qubits so that the code’s logical qubit carries it.
The smallest code that shows the step is the three-qubit repetition code,
with logical states |000> and |111>. Injected, the state is
Encoding takes a CNOT from the qubit that holds |T> to each of the
other two, and a CNOT needs a two-qubit interaction the pulses can use. On
transmon_surface17 the neighbours of D5 are 2 GHz away from it, too
far for a drive to make its few megahertz of coupling act, and the chip’s
own two-qubit gate, a flux pulse, isn’t simulated (see
The transmon_surface17 Quantum System).
So describe a chip where a drive is enough: three transmons in a row,
100 MHz apart, each coupled to the next, as Describe Your Own Quantum System
builds a pair. The middle one will hold |T>:
from cudaq_pulse.quantum_systems import (
Coupling,
DispersiveReadout,
DragPulse,
PhysicalQudit,
ReadoutPulse,
)
def transmon(index, frequency_hz, thermal_population=0.0):
return PhysicalQudit(
index=index,
frequency_hz=frequency_hz,
t1_us=100.0,
t2_star_us=80.0,
anharmonicity_hz=-2.2e8,
thermal_population=thermal_population,
readout=DispersiveReadout(error_1to0=0.01, error_0to1=0.005),
calibrations={
pulse.gates.X: DragPulse(duration_ns=40.0, sigma_ns=10.0),
pulse.gates.READOUT: ReadoutPulse(duration_ns=300.0, amplitude=0.05),
},
)
# The middle qubit takes |T>. It starts as warm as D5; its neighbours, the
# rest of the code, start cold.
chain = pulse.quantum_system(
name="t_chain",
qudits={
0: transmon(0, 5.15e9),
1: transmon(1, 5.25e9, thermal_population=0.027),
2: transmon(2, 5.35e9),
},
couplings=[
Coupling((0, 1), strength_hz=4.0e6),
Coupling((1, 2), strength_hz=4.0e6),
],
)
first, source, second = chain.qudits
# Two levels per transmon, in steps of 0.5 ns: 20 to a period of the
# 100 MHz between neighbours.
CHAIN = {"levels": 2, "dt_ns": 0.5}
The encoded state gets a tomography setting of its own: the axis |T>
points along.
T_AXIS = 3
What is and isn’t simulated on this chain:
the pulses, the always-on couplings, decay and dephasing, and the middle qubit’s thermal start;
two levels per transmon, so no leakage to a third level;
the two outer qubits start in
|0>.
A CNOT from a cross-resonance pulse#
Drive the control qubit’s line at the target’s tone. Through the
coupling, the target turns, one way if the control is in |0> and the
other way if it is in |1>. That is a cross-resonance pulse.
controlled_turn plays it twice, the second time reversed, with a π
pulse on the control before and after the second: what the drive does to
the control itself cancels, and the turn of the target adds up. cnot
follows it with a quarter turn of the target about the same axis. With the
control in |0> the two quarter turns cancel, and with the control in
|1> they add to a flip:
@pulse.kernel
def controlled_turn(control, target, amplitude: float):
"""An echoed cross-resonance pulse: the control's line is driven at the
target's tone, and the target turns one way with the control in |0> and
the other way with it in |1>. The pi pulse between the two halves, with
the second half reversed, cancels what the drive does to the control
itself."""
control_drive = get_drive_line(control)
target_drive = get_drive_line(target)
flip = control_drive.get_calibration(pulse.gates.X)
cross = gaussian_square(180 * pulse.ns, amplitude, 10 * pulse.ns, 100 * pulse.ns)
back = gaussian_square(180 * pulse.ns, -amplitude, 10 * pulse.ns, 100 * pulse.ns)
sync(control_drive, target_drive)
drive(control_drive, cross, tone=target_drive.tone)
drive(control_drive, flip)
drive(control_drive, back, tone=target_drive.tone)
drive(control_drive, flip)
sync(control_drive, target_drive)
@pulse.kernel
def cnot(control, target, amplitude: float, axis_rad: float):
"""The controlled turn, a quarter turn each way, then a quarter turn of
the target about the same axis: none in all, or a half turn."""
controlled_turn(control, target, amplitude)
target_drive = get_drive_line(target)
# With its tone's phase moved by -axis_rad, a pulse turns about the axis
# at axis_rad.
shift_phase(target_drive.tone, -axis_rad)
drive(target_drive, target_drive.get_calibration(pulse.gates.X90))
shift_phase(target_drive.tone, axis_rad)
The CNOT has two numbers to calibrate, the amplitude of the pulse and the axis of the target’s quarter turn. Each comes from simulated runs, as it would from measured ones:
@pulse.kernel
def turn_check(control, target, amplitude: float):
controlled_turn(control, target, amplitude)
@pulse.kernel
def cnot_check(control, target, excite: int, amplitude: float, axis_rad: float):
control_drive = get_drive_line(control)
if excite:
drive(control_drive, control_drive.get_calibration(pulse.gates.X))
cnot(control, target, amplitude, axis_rad)
def target_excited(kernel, target, *args):
pair = np.zeros(4)
pair[0] = 1.0
result = pulse.simulate(kernel, source, target, *args, initial_state=pair, **CHAIN)
return float(result.level_populations(target)[1])
def calibrate_cnot(target):
"""The two numbers of the CNOT from ``source`` to *target*, each from
simulated runs, as a calibration measures them."""
# The amplitude of a quarter turn: the target ends half excited.
amplitude = 0.15
for _ in range(2):
excited = target_excited(turn_check, target, amplitude)
amplitude *= (math.pi / 2) / (2 * math.asin(math.sqrt(excited)))
# The axis of the turn: the phase of the target's own quarter turn that
# brings it back to |0> when the control is in |0>.
along = target_excited(cnot_check, target, 0, amplitude, 0.0)
across = target_excited(cnot_check, target, 0, amplitude, math.pi / 2)
axis_rad = math.atan2(across - 0.5, along - 0.5) + math.pi
return amplitude, axis_rad
Calibrate the CNOT to each outer qubit, and check it: prepare the control
in |0> or |1> and see how often the target ends flipped.
cnots = {}
print("target amplitude axis (deg) flipped: control 0, control 1")
for name, target in (("first", first), ("second", second)):
cnots[name] = calibrate_cnot(target)
amplitude, axis_rad = cnots[name]
flipped = []
for excite in (0, 1):
share = target_excited(cnot_check, target, excite, *cnots[name])
flipped.append(share)
print(
f"{name:6} {amplitude:9.4f} {math.degrees(axis_rad) % 360:10.1f} "
f"{flipped[0]:18.3f} {flipped[1]:9.3f}"
)
Output
target amplitude axis (deg) flipped: control 0, control 1
first 0.1624 0.1 0.002 0.997
second 0.1624 179.9 0.002 0.997
What to notice:
the target flips on 99.7% of shots with the control in
|1>, and on 0.2% with it in|0>. The gate takes 480 ns, and what is missing is the decay and dephasing of that time;the two targets turn about opposite axes, 0° and 180°: one sits below the control in frequency and the other above.
Inject the state into the code#
encode_and_measure prepares the state on the middle qubit, copies its
0 or 1 onto each neighbour with a CNOT, and measures the encoded qubit:
@pulse.kernel
def encode_and_measure(
source,
first,
second,
first_amplitude: float,
first_axis_rad: float,
second_amplitude: float,
second_axis_rad: float,
axis: int,
):
source_drive = get_drive_line(source)
drives = [get_drive_line(first), source_drive, get_drive_line(second)]
readouts = [get_readout_line(first), get_readout_line(source), get_readout_line(second)]
# The state to inject.
drive(source_drive, source_drive.get_calibration(pulse.gates.Y90))
drive(source_drive, source_drive.get_calibration(pulse.gates.T))
# Copy the source's 0 or 1 onto each neighbour: a|000> + b|111>.
cnot(source, first, first_amplitude, first_axis_rad)
cnot(source, second, second_amplitude, second_axis_rad)
# Two settings. Z reads each qubit as it is. T takes the source's tone
# back by the T gate's pi / 4 and reads X on all three, X_L: the axis
# the injected state points along.
sync(drives[0], drives[1], drives[2])
if axis == T_AXIS:
shift_phase(source_drive.tone, -math.pi / 4)
for line in drives:
y90 = line.get_calibration(pulse.gates.Y90)
drive(line, y90.replace(amplitude=-y90.amplitude))
sync(drives[0], drives[1], drives[2], readouts[0], readouts[1], readouts[2])
for line in readouts:
readout(line, line.get_calibration(pulse.gates.READOUT))
The encoded qubit is measured in two settings:
Z reads the three qubits as they are. In the code space the three readouts agree;
T reads the logical operator XL, X on all three qubits, after taking the middle qubit’s tone back by the T gate’s π/4. That is the axis the injected state points along. A product of Paulis reads +1 or -1 by the parity of its readouts.
Together they give the fidelity with the injected state,
(|000> + eiπ/4 |111>)/√2. The Z setting reads the populations of
000 and 111, the share of shots in the code space. The T setting’s parity
reads twice the coherence between them. The fidelity is half their sum.
The parity alone is not enough: (1 + parity)/2 is the weight of the state on one side of XL, which states outside the code space share, so it reads higher than the fidelity.
def code_readouts(branch):
"""The last three readouts a shot took: first, source, second."""
return [min(level, 1) for level in branch.levels if level >= 0][-3:]
def parity(result):
"""The mean of +1 or -1 by the parity of the three readouts: what a
product of Paulis on the three qubits reads."""
return sum((-1) ** sum(code_readouts(b)) * b.probability for b in result.branches)
def in_code_space(result):
"""The share of shots whose three readouts agree: 000 or 111."""
return sum(b.probability for b in result.branches if len(set(code_readouts(b))) == 1)
def encoded_fidelity(z_setting, t_setting):
"""The fidelity with (|000> + e^{i pi/4}|111>) / sqrt(2) from the two
settings: the populations of 000 and 111, which the Z setting reads,
and twice the coherence between them, which the T setting's parity
reads, halved."""
return (in_code_space(z_setting) + parity(t_setting)) / 2
Now put the page together. injected_t repeats the herald on the middle
qubit until it succeeds, as heralded_t does, and then encodes:
@pulse.kernel
def injected_t(
source,
first,
second,
attempts: int,
first_amplitude: float,
first_axis_rad: float,
second_amplitude: float,
second_axis_rad: float,
axis: int,
):
drive_line = get_drive_line(source)
readout_line = get_readout_line(source)
outcome = readout(readout_line, readout_line.get_calibration(pulse.gates.READOUT))
request = herald.submit([outcome])
accepted = request.result()
sync(drive_line, readout_line)
if accepted:
encode_and_measure(
source,
first,
second,
first_amplitude,
first_axis_rad,
second_amplitude,
second_axis_rad,
axis,
)
else:
drive(drive_line, drive_line.get_calibration(pulse.gates.X))
sync(drive_line, readout_line)
if attempts > 1:
injected_t(
source,
first,
second,
attempts - 1,
first_amplitude,
first_axis_rad,
second_amplitude,
second_axis_rad,
axis,
)
Run it with at most two attempts, keep the shots that succeeded, and measure the encoded qubit:
compiled_injection = pulse.compile(
injected_t,
quantum_system=chain,
control_system=control_system,
)
first_amplitude, first_axis_rad = cnots["first"]
second_amplitude, second_axis_rad = cnots["second"]
def injected(axis, mode="ideal"):
program = compiled_injection.lower(
source,
first,
second,
2,
first_amplitude,
first_axis_rad,
second_amplitude,
second_axis_rad,
axis,
)
result = pulse.simulate(program, readout=mode, **CHAIN)
return result.postselect(succeeded)
z_setting = injected(Z_AXIS)
t_setting = injected(T_AXIS)
fidelity_ideal = encoded_fidelity(z_setting, t_setting)
print(f"shots that succeeded: {z_setting.acceptance:.4f}")
print(f"in the code space: {in_code_space(z_setting):.3f}")
print(f"parity along T: {parity(t_setting):.3f}")
print(f"fidelity with |T_L>: {fidelity_ideal:.3f}")
Output
shots that succeeded: 0.9999
in the code space: 0.986
parity along T: 0.966
fidelity with |T_L>: 0.976
What to notice:
98.6% of the shots are in the code space;
the encoded qubit points along the axis
|T>did on the physical qubit: the T gate’s phase shift went through the two CNOTs, and the echo left no phase of its own on the control;its fidelity with the injected state is 0.976, half of 0.986 + 0.966.
Those are with the ideal readout. Through the readout model, the herald and the three readouts can each misreport. Measure both settings again:
z_model = injected(Z_AXIS, "model")
t_model = injected(T_AXIS, "model")
fidelity_model = encoded_fidelity(z_model, t_model)
print(f"shots that succeeded: {z_model.acceptance:.4f}")
print(f"in the code space: {in_code_space(z_model):.3f}")
print(f"parity along T: {parity(t_model):.3f}")
print(f"fidelity with |T_L>: {fidelity_model:.3f}")
Output
shots that succeeded: 0.9950
in the code space: 0.964
parity along T: 0.922
fidelity with |T_L>: 0.943
What to notice: through the readout model the same state reads 0.943. The difference is the three readouts of the tomography, each wrong on about 1% of shots.
Here the state does wait. The two CNOTs take almost a microsecond, during which three qubits with a T2* of 80 µs hold one entangled state, and that is where most of the missing 2.4% goes. The pulses of the physical state cost 2e-5.
Where to go next#
examples/28_t_state_injection.py holds this page’s kernels in one script that runs in about a second: it reads the injection’s fidelity from the two settings after one herald. It also makes the same
|T>with one X90 about the axis at 3π/4, and measures the tomography with sampled shots.examples/19_classical_calls.py sends readouts to classical functions and branches on their answers, the mechanism the herald uses.
examples/08_readout_and_branching.py shows the active reset each attempt’s correcting pulse is built from.
examples/02_two_qubit_cross_resonance.py plays a cross-resonance pulse sequence on the 17-qubit model.
For the details:
Phase frames and virtual Z gates gives the phase gates’ sign convention;
Call Classical Code During a Shot covers classical functions, links and their timing reports;
Simulation covers how simulations follow measurement branches and post-select on them.