Control-FlowΒΆ

Download this notebook - control-flow.ipynb

from guppylang import guppy
from guppylang.std.builtins import result
from guppylang.std.quantum import measure, qubit, discard, h, tdg, cx, t, z

Implement an \(V_3 = R_z(-2\arctan(2)) = (I + 2iZ)/\sqrt{5}\) gate using the repeat-until-success scheme from https://arxiv.org/abs/1311.1074.

@guppy
def repeat_until_success(q: qubit) -> None:
    attempts = 0
    while True:
        attempts += 1

        # Prepare ancillas
        a, b = qubit(), qubit()
        h(a)
        h(b)

        tdg(a)
        cx(b, a)
        t(a)
        h(a)
        if measure(a):
            # First part failed, try again
            discard(b)
            continue

        t(q)
        z(q)
        cx(q, b)
        t(b)
        h(b)
        if measure(b):
            # Second part failed, apply correction
            # and try again
            z(q)
            continue

        result("attempts", attempts)
        break
@guppy
def main() -> None:
    q = qubit()
    repeat_until_success(q)
    measure(q)
shots = main.emulator(n_qubits=3).with_seed(0).with_shots(100).run()
sum(int(shot.as_dict()["attempts"]) for shot in shots) / len(shots)
1.49