Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b977d66
docs: add some docstrings for qsystem gates
CalMacCQ Apr 8, 2025
0f20f61
fix a typo
CalMacCQ Apr 8, 2025
0d538b7
minor fixes
CalMacCQ Apr 8, 2025
a43628e
edit measure docstring
CalMacCQ Apr 8, 2025
c5b1928
add a doctest
CalMacCQ Apr 8, 2025
9252486
Merge branch 'main' into cm/qsystem_gates
CalMacCQ Apr 17, 2025
311af2c
rm whitespace
CalMacCQ Apr 17, 2025
e7c6052
Merge branch 'main' into cm/qsystem_gates
CalMacCQ Jun 16, 2025
a0662d3
update ZZPhase
CalMacCQ Jul 2, 2025
1001436
add module docstring for qsystem.functional
CalMacCQ Jul 2, 2025
e01cbc1
add a bunch of gate defs to std.quantum
CalMacCQ Jul 2, 2025
e96b130
more definitions
CalMacCQ Jul 2, 2025
7f317bc
small addition
CalMacCQ Jul 2, 2025
39c60ce
add docstrings for functional modules
CalMacCQ Jul 2, 2025
d1abab9
fix ch syntax
CalMacCQ Jul 2, 2025
c2a83d7
Update guppylang/std/qsystem/__init__.py
CalMacCQ Jul 4, 2025
5515c13
Update guppylang/std/qsystem/__init__.py
CalMacCQ Jul 4, 2025
5fbbd6c
Update guppylang/std/qsystem/__init__.py
CalMacCQ Jul 4, 2025
827b26e
Update guppylang/std/quantum/__init__.py
CalMacCQ Jul 4, 2025
ac9b8c8
Update guppylang/std/qsystem/functional.py
CalMacCQ Jul 4, 2025
109f294
apply Seyon's suggestions in quantum
CalMacCQ Jul 16, 2025
7c730be
update qsystem
CalMacCQ Jul 16, 2025
035db79
minor fix
CalMacCQ Jul 16, 2025
9121469
small fix
CalMacCQ Jul 16, 2025
cab6315
Update guppylang/std/qsystem/__init__.py
CalMacCQ Jul 17, 2025
6c5eca5
zz_max consistency
CalMacCQ Jul 17, 2025
3483b5e
CRz fix
CalMacCQ Jul 17, 2025
e683ab0
don't use pi factors in matrix definitions for parametrised gates
CalMacCQ Jul 17, 2025
72983c2
fix ry definition
CalMacCQ Jul 17, 2025
7581a8e
Update guppylang/std/quantum/functional.py
CalMacCQ Jul 17, 2025
e1f8b10
Update guppylang/std/quantum/__init__.py
CalMacCQ Jul 17, 2025
06d64ef
Update guppylang/std/quantum/__init__.py
CalMacCQ Jul 17, 2025
1188e13
fix missing pi factors
CalMacCQ Jul 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions guppylang/std/qsystem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
@guppy
@no_type_check
def phased_x(q: qubit, angle1: angle, angle2: angle) -> None:
r"""phased_x gate command.

.. math::

\mathrm{phased\_x}(q, \theta_1, \theta_2)=
\mathrm{Rz(\theta_2)Rx(\theta_1)Rz(\theta_2)}&=
\begin{pmatrix}
\cos(\frac{\pi \theta_1}{2}) &
-i e^{-i \pi \theta_2}\sin(\frac{\pi\theta_1}{2})\\
-i e^{i \pi \theta_2}\sin(\frac{\pi\theta_1}{2}) &
\cos(\frac{\pi \theta_1}{2})
\end{pmatrix}
"""
f1 = float(angle1)
f2 = float(angle2)
_phased_x(q, f1, f2)
Expand All @@ -25,30 +38,72 @@ def phased_x(q: qubit, angle1: angle, angle2: angle) -> None:
@guppy
@no_type_check
def zz_max(q1: qubit, q2: qubit) -> None:
"""Maximally entangling ZZPhase.

Equivalent to `zz_phase(q1, q2, pi / 2)`.
r"""zz_max gate command. A maximally entangling zz_phase gate.

This is a special case of the ZZPhase operation with angle = pi/2

.. math::
\mathrm{zz\_max}(q_1,q_2)=\mathrm{zz\_phase}(q_2,q_1,\pi/2)=
\exp(\frac{- i \pi}{4}\big(Z \otimes Z \big))=
\begin{pmatrix}
e^{\frac{-i \pi}{4}} & 0 & 0 & 0 \\
0 & e^{\frac{i \pi}{4}} & 0 & 0 \\
0 & 0 & e^{\frac{i \pi}{4}} & 0 \\
0 & 0 & 0 & e^{\frac{-i \pi}{4}}
\end{pmatrix}
"""
zz_phase(q1, q2, pi / 2)


@guppy
@no_type_check
def zz_phase(q1: qubit, q2: qubit, angle: angle) -> None:
r"""zz_phase gate command.

.. math::
\mathrm{zz\_phase}(q_1,q_2,\theta)=
\exp(\frac{- i \pi \theta}{2}\big(Z \otimes Z \big))=
\begin{pmatrix}
e^{\frac{-i \pi \theta}{2}} & 0 & 0 & 0 \\
0 & e^{\frac{i \pi \theta}{2}} & 0 & 0 \\
0 & 0 & e^{\frac{i \pi \theta}{2}} & 0 \\
0 & 0 & 0 & e^{\frac{-i \pi \theta}{2}}
\end{pmatrix}
>>> @guppy
... def qsystem_cx(q1: qubit, q2: qubit) -> None:
... phased_x(angle(3/2), angle(-1/2), q2)
... zz_phase(q1, q2, angle(1/2))
... rz(angle(5/2), q1)
... phasedx(angle(-3/2), q1)
... rz(angle(3/2), q2)
>>> guppy.compile(qsystem_cx)
"""
f = float(angle)
_zz_phase(q1, q2, f)


@guppy
@no_type_check
def rz(q: qubit, angle: angle) -> None:
r"""rz gate command.

.. math::
\mathrm{rz}(q,\theta)=
\exp(\frac{- i \pi \theta}{2}\big(Z \big))=
\begin{pmatrix}
e^{\frac{-i \pi \theta}{2}} & 0 \\
0 & e^{\frac{i \pi \theta}{2}}
\end{pmatrix}
"""

f1 = float(angle)
_rz(q, f1)


@guppy.hugr_op(quantum_op("Measure", ext=QSYSTEM_EXTENSION))
@no_type_check
def measure(q: qubit @ owned) -> bool: ...
def measure(q: qubit @ owned) -> bool:
"""Measure a qubit destructively."""


@guppy.custom(InoutMeasureResetCompiler("MeasureReset", QSYSTEM_EXTENSION))
Expand All @@ -59,7 +114,8 @@ def measure_and_reset(q: qubit) -> bool:

@guppy.hugr_op(quantum_op("Reset", ext=QSYSTEM_EXTENSION))
@no_type_check
def reset(q: qubit) -> None: ...
def reset(q: qubit) -> None:
"""Reset a qubit to the |0> state."""


# TODO
Expand Down Expand Up @@ -94,7 +150,7 @@ def _phased_x(q: qubit, angle1: float, angle2: float) -> None:
def _zz_phase(q1: qubit, q2: qubit, angle: float) -> None:
"""ZZPhase operation from the qsystem extension.

See `guppylang.std.qsystem.phased_x` for a public definition that
See `guppylang.std.qsystem.zz_phase` for a public definition that
accepts angle parameters.
"""

Expand Down
14 changes: 14 additions & 0 deletions guppylang/std/qsystem/functional.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""Guppy standard module for functional qsystem native operations. For the mathematical
definitions of these gates, see the guppylang.std.qsystem documentation.
These gates are the same as those in the std.qsystem but use functional syntax.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
These gates are the same as those in the std.qsystem but use functional syntax.
These gates are the same as those in std.qsystem but use functional syntax.

"""

from typing import no_type_check

from guppylang.decorator import guppy
Expand All @@ -10,53 +16,61 @@
@guppy
@no_type_check
def phased_x(q: qubit @ owned, angle1: angle, angle2: angle) -> qubit:
"""Functional PhasedX gate command."""
qsystem.phased_x(q, angle1, angle2)
return q


@guppy
@no_type_check
def zz_phase(q1: qubit @ owned, q2: qubit @ owned, angle: angle) -> tuple[qubit, qubit]:
"""Functional ZZPhase gate command."""
qsystem.zz_phase(q1, q2, angle)
return q1, q2


@guppy
@no_type_check
def measure_and_reset(q: qubit @ owned) -> tuple[qubit, bool]:
"""Functional measure_and_reset command."""
b = qsystem.measure_and_reset(q)
return q, b


@guppy
@no_type_check
def zz_max(q1: qubit @ owned, q2: qubit @ owned) -> tuple[qubit, qubit]:
"""Functional ZZMax gate command."""
qsystem.zz_max(q1, q2)
return q1, q2


@guppy
@no_type_check
def rz(q: qubit @ owned, angle: angle) -> qubit:
"""Functional Rz gate command."""
qsystem.rz(q, angle)
return q


@guppy
@no_type_check
def measure(q: qubit @ owned) -> bool:
"""Functional destructive measurement command."""
result = qsystem.measure(q)
return result


@guppy
@no_type_check
def qfree(q: qubit @ owned) -> None:
"""Functional qfree command."""
qsystem.qfree(q)


@guppy
@no_type_check
def reset(q: qubit @ owned) -> qubit:
"""Functional Reset command."""
qsystem.reset(q)
return q
Loading
Loading