Skip to content

Commit

Permalink
refactor: update T factory example (#817)
Browse files Browse the repository at this point in the history
closes #813 

Replacing all `list` usage in the `t_factory` example. 

I've used arrays in the `distill` function and an `Option[qubit]` return
in `t_state` which I think is much nicer.
  • Loading branch information
CalMacCQ authored Feb 28, 2025
1 parent 75efd22 commit 11338f0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
64 changes: 28 additions & 36 deletions examples/t_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,50 @@

from guppylang.decorator import guppy
from guppylang.std.angles import angle, pi
from guppylang.std.builtins import owned, py
from guppylang.std.builtins import array, owned, py
from guppylang.std.option import Option, nothing, some
from guppylang.std.quantum import (
cz,
discard,
h,
measure,
qubit,
)
from guppylang.std.quantum_functional import (
cz,
h,
rx,
ry,
rz,
)

phi = np.arccos(1 / 3)


@guppy
def ry(q: qubit @owned, theta: angle) -> qubit:
q = rx(q, pi / 2)
q = rz(q, theta + pi)
q = rx(q, pi / 2)
return rz(q, pi)

# Preparation of approximate T state, from https://arxiv.org/abs/2310.12106
@guppy
def prepare_approx(q: qubit @owned) -> qubit:
q = ry(q, angle(py(phi)))
return rz(q, pi / 4)
def prepare_approx() -> qubit:
q = qubit()
ry(q, angle(py(phi)))
rz(q, pi / 4)
return q


# The inverse of the [[5,3,1]] encoder in figure 3 of https://arxiv.org/abs/2208.01863
@guppy
def distill(
target: qubit @owned, q0: qubit @owned, q1: qubit @owned, q2: qubit @owned, q3: qubit @owned
target: qubit @ owned,
qs: array[qubit, 4] @ owned,
) -> tuple[qubit, bool]:
"""First argument is the target qubit which will be returned from the circuit.
Other arguments are ancillae, which should also be in an approximate T state.
Returns target qubit and a bool, which is true if the distillation succeeded.
"""
q0, q1 = cz(q0, q1)
q2, q3 = cz(q2, q3)
target, q0 = cz(target, q0)
q1, q2 = cz(q1, q2)
target, q3 = cz(target, q3)
cz(qs[0], qs[1])
cz(qs[2], qs[3])
cz(target, qs[0])
cz(qs[1], qs[2])
cz(target, qs[3])
# Measuring gives false for success, true for failure.
# We check for all falses to say whether distillation succeeded.
bits = [not (measure(h(q))) for q in [q0, q1, q2, q3]]
for i in range(4):
h(qs[i])
bits = array(not measure(q) for q in qs)
# guppy doesn't yet support the `any` or `all` operators...
success = True
for b in bits:
Expand All @@ -57,33 +54,28 @@ def distill(


@guppy
def t_state(timeout: int) -> tuple[list[qubit], bool]:
def t_state(timeout: int) -> Option[qubit]:
"""Create a T state using magic state distillation with `timeout` attempts.
On success returns a singleton `linst` containing a qubit in a magic T state
and the boolean `True`.
On success returns a qubit in a magic T state.
If the number of attempts is exceeded, the empty `linst` will be returned
along with the boolean `False`.
On failure (i.e. number of attempts are exceeded) returns nothing.
"""
if timeout > 0:
tgt = prepare_approx(qubit())
q0 = prepare_approx(qubit())
q1 = prepare_approx(qubit())
q2 = prepare_approx(qubit())
q3 = prepare_approx(qubit())
tgt = prepare_approx()
qs = array(prepare_approx() for _ in range(4))

q, success = distill(tgt, q0, q1, q2, q3)
q, success = distill(tgt, qs)
if success:
return [q], True
return some(q)
else:
# Discard the qubit and start over
# Note, this could just as easily be a while loop!
discard(q)
return t_state(timeout - 1)

# We ran out of attempts
return [], False
return nothing()


hugr = guppy.compile_module()
File renamed without changes.
3 changes: 1 addition & 2 deletions tests/integration/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Tests validating the files in the `examples` directory."""



def test_demo_notebook(nb_regression):
nb_regression.diff_ignore += ("/metadata/language_info/version",)
nb_regression.check("examples/demo.ipynb")
nb_regression.check("tests/integration/notebooks/demo.ipynb")


def test_random_walk_qpe(validate):
Expand Down

0 comments on commit 11338f0

Please sign in to comment.