diff --git a/mitiq/interface/mitiq_pyquil/compiler.py b/mitiq/interface/mitiq_pyquil/compiler.py index e98c2079eb..6fc8b83e83 100644 --- a/mitiq/interface/mitiq_pyquil/compiler.py +++ b/mitiq/interface/mitiq_pyquil/compiler.py @@ -10,19 +10,18 @@ and modified to support a larger gateset (e.g. CPHASE). """ -from math import pi -from typing import Any, Union, cast +from typing import cast import numpy as np from pyquil.gates import CZ, RX, RZ, XY, I -from pyquil.quil import FormalArgument, Program, Qubit, QubitPlaceholder -from pyquil.quilbase import Expression, Gate +from pyquil.quil import Program +from pyquil.quilatom import ParameterDesignator, QubitDesignator +from pyquil.quilbase import Gate -QubitLike = Union[Qubit, QubitPlaceholder, FormalArgument] -AngleLike = Union[Expression, Any, complex] - -def _CCNOT(q0: QubitLike, q1: QubitLike, q2: QubitLike) -> Program: +def _CCNOT( + q0: QubitDesignator, q1: QubitDesignator, q2: QubitDesignator +) -> Program: """ A CCNOT in terms of RX(+-pi/2), RZ(theta), and CZ @@ -50,7 +49,7 @@ def _CCNOT(q0: QubitLike, q1: QubitLike, q2: QubitLike) -> Program: return p -def _CNOT(q0: QubitLike, q1: QubitLike) -> Program: +def _CNOT(q0: QubitDesignator, q1: QubitDesignator) -> Program: """ A CNOT in terms of RX(+-pi/2), RZ(theta), and CZ @@ -65,7 +64,9 @@ def _CNOT(q0: QubitLike, q1: QubitLike) -> Program: return p -def _CPHASE(angle: AngleLike, q0: QubitLike, q1: QubitLike) -> Program: +def _CPHASE( + angle: ParameterDesignator, q0: QubitDesignator, q1: QubitDesignator +) -> Program: """ from quilc: @@ -85,7 +86,7 @@ def _CPHASE(angle: AngleLike, q0: QubitLike, q1: QubitLike) -> Program: return p -def _H(q: QubitLike) -> Program: +def _H(q: QubitDesignator) -> Program: """ A Hadamard in terms of RX(+-pi/2) and RZ(theta) @@ -98,7 +99,7 @@ def _H(q: QubitLike) -> Program: return p -def _ISWAP(q0: QubitLike, q1: QubitLike) -> Program: +def _ISWAP(q0: QubitDesignator, q1: QubitDesignator) -> Program: """ An ISWAP as an XY(pi). Of course, assumes XY is available. """ @@ -107,7 +108,7 @@ def _ISWAP(q0: QubitLike, q1: QubitLike) -> Program: return p -def _PHASE(angle: AngleLike, q: QubitLike) -> Program: +def _PHASE(angle: ParameterDesignator, q: QubitDesignator) -> Program: """ from quilc: @@ -119,38 +120,38 @@ def _PHASE(angle: AngleLike, q: QubitLike) -> Program: return p -def _RX(angle: AngleLike, q: QubitLike) -> Program: +def _RX(angle: ParameterDesignator, q: QubitDesignator) -> Program: """ A RX in terms of native RX(+-pi/2) and RZ gates. """ p = Program() - p += RZ(pi / 2, q) - p += RX(pi / 2, q) + p += RZ(np.pi / 2, q) + p += RX(np.pi / 2, q) p += RZ(angle, q) - p += RX(-pi / 2, q) - p += RZ(-pi / 2, q) + p += RX(-np.pi / 2, q) + p += RZ(-np.pi / 2, q) return p -def _RY(angle: AngleLike, q: QubitLike) -> Program: +def _RY(angle: ParameterDesignator, q: QubitDesignator) -> Program: """ A RY in terms of RX(+-pi/2) and RZ(theta) """ p = Program() - p += RX(pi / 2, q) + p += RX(np.pi / 2, q) p += RZ(angle, q) - p += RX(-pi / 2, q) + p += RX(-np.pi / 2, q) return p -def _S(q: QubitLike) -> Program: +def _S(q: QubitDesignator) -> Program: """ An S in terms of RZ(theta) """ return Program(RZ(np.pi / 2, q)) -def _SWAP(q0: QubitLike, q1: QubitLike) -> Program: +def _SWAP(q0: QubitDesignator, q1: QubitDesignator) -> Program: """ A SWAP in terms of _CNOT @@ -165,7 +166,7 @@ def _SWAP(q0: QubitLike, q1: QubitLike) -> Program: return p -def _T(q: QubitLike, dagger: bool = False) -> Program: +def _T(q: QubitDesignator, dagger: bool = False) -> Program: """ A T in terms of RZ(theta) """ @@ -175,7 +176,7 @@ def _T(q: QubitLike, dagger: bool = False) -> Program: return Program(RZ(np.pi / 4, q)) -def _X(q: QubitLike) -> Program: +def _X(q: QubitDesignator) -> Program: """ An X in terms of RX(pi) @@ -187,7 +188,7 @@ def _X(q: QubitLike) -> Program: return p -def _Y(q: QubitLike) -> Program: +def _Y(q: QubitDesignator) -> Program: """ A Y in terms of _RY """ @@ -196,7 +197,7 @@ def _Y(q: QubitLike) -> Program: return p -def _Z(q: QubitLike) -> Program: +def _Z(q: QubitDesignator) -> Program: """ A Z in terms of RZ """ @@ -210,8 +211,8 @@ def is_magic_angle(angle: complex) -> bool: Checks to see if an angle is 0, +/-pi/2, or +/-pi. """ return bool( - np.isclose(np.abs(angle), pi / 2) - or np.isclose(np.abs(angle), pi) + np.isclose(np.abs(angle), np.pi / 2) + or np.isclose(np.abs(angle), np.pi) or np.isclose(angle, 0.0) ) diff --git a/mitiq/interface/mitiq_pyquil/tests/test_conversions_pyquil.py b/mitiq/interface/mitiq_pyquil/tests/test_conversions_pyquil.py index 26f7abdd6a..2dc49d415e 100644 --- a/mitiq/interface/mitiq_pyquil/tests/test_conversions_pyquil.py +++ b/mitiq/interface/mitiq_pyquil/tests/test_conversions_pyquil.py @@ -60,27 +60,24 @@ def test_to_pyquil_from_pyquil_parameterized(): X 0 Y 1 Z 2 -RX(pi/2) 0 -RY(pi/2) 1 -RZ(pi/2) 2 +RX(1.5707963267948966) 0 +RY(1.5707963267948966) 1 +RZ(1.5707963267948966) 2 H 0 CZ 0 1 CNOT 1 2 -CPHASE(pi/2) 0 1 -CPHASE00(pi/2) 1 2 -CPHASE01(pi/2) 0 1 -CPHASE10(pi/2) 1 2 +CPHASE(1.5707963267948966) 0 1 ISWAP 0 1 SWAP 1 2 -XY(pi/2) 0 1 +XY(1.5707963267948966) 0 1 CCNOT 0 1 2 CSWAP 0 1 2 """ def test_to_pyquil_from_pyquil_almost_all_gates(): - """PHASE, PSWAP, S, T, declaration, and measurement don't convert back - and forth perfectly (in terms of labels -- the program unitaries and + """PHASE, PSWAP, CHPASEij, S, T, declaration, and measurement don't convert + back and forth perfectly (in terms of labels -- the program unitaries and number of measurements are equivalent).""" p = Program(MEASURELESS_QUIL_PROGRAM) assert p.out() == to_pyquil(from_pyquil(p)).out() diff --git a/requirements/requirements-braket.txt b/requirements/requirements-braket.txt index b9d45d00dd..9ca7d1d880 100644 --- a/requirements/requirements-braket.txt +++ b/requirements/requirements-braket.txt @@ -1,2 +1,2 @@ amazon-braket-sdk~=1.69.0 -cirq-ionq>=1.4.0,<1.5.0 \ No newline at end of file +cirq-ionq>=1.4.0,<1.5.0 diff --git a/requirements/requirements-pyquil.txt b/requirements/requirements-pyquil.txt index 9e0041b3ed..ab4f6faca9 100644 --- a/requirements/requirements-pyquil.txt +++ b/requirements/requirements-pyquil.txt @@ -1,2 +1,2 @@ -pyquil~=3.5.4 -cirq-rigetti>=1.4.0,<1.5.0 \ No newline at end of file +pyquil~=4.11.0 +cirq-rigetti>=1.4.0,<1.5.0