Skip to content

Commit

Permalink
move SU2RotationGate to basic_gates/
Browse files Browse the repository at this point in the history
  • Loading branch information
anurudhp committed Mar 18, 2024
1 parent 7f4ee65 commit ce193f2
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 67 deletions.
73 changes: 73 additions & 0 deletions qualtran/bloqs/basic_gates/su2_rotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from functools import cached_property

import cirq
import numpy as np
from attrs import frozen
from numpy.typing import NDArray

from qualtran import GateWithRegisters, Signature


@frozen
class SU2RotationGate(GateWithRegisters):
r"""Implements an arbitrary SU(2) rotation.
The rotation is represented by the matrix:
$$
\begin{matrix}
e^{i(\lambda + \phi)} \cos(\theta) & e^{i\phi} \sin(\theta) \\
e^{i\lambda} \sin(\theta) & - \cos(\theta)
\end{matrix}
$$
Returns:
A 2x2 rotation matrix
References:
[Generalized Quantum Signal Processing](https://arxiv.org/abs/2308.01501)
Motlagh and Wiebe. (2023). Equation 7.
"""

theta: float
phi: float
lambd: float

@cached_property
def signature(self) -> Signature:
return Signature.build(q=1)

@cached_property
def rotation_matrix(self):
return np.array(
[
[
np.exp(1j * (self.lambd + self.phi)) * np.cos(self.theta),
np.exp(1j * self.phi) * np.sin(self.theta),
],
[np.exp(1j * self.lambd) * np.sin(self.theta), -np.cos(self.theta)],
]
)

def decompose_from_registers(
self, *, context: cirq.DecompositionContext, q: NDArray[cirq.Qid]
) -> cirq.OP_TREE:
qubit = q[0]

yield cirq.Rz(rads=np.pi - self.lambd).on(qubit)
yield cirq.Ry(rads=2 * self.theta).on(qubit)
yield cirq.Rz(rads=-self.phi).on(qubit)
yield cirq.GlobalPhaseGate(np.exp(1j * (np.pi + self.lambd + self.phi) / 2)).on()
31 changes: 31 additions & 0 deletions qualtran/bloqs/basic_gates/su2_rotation_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import cirq
from .su2_rotation import SU2RotationGate


def test_cirq_decompose_SU2_to_single_qubit_pauli_gates():
random_state = np.random.default_rng(42)

for _ in range(20):
theta = random_state.random() * 2 * np.pi
phi = random_state.random() * 2 * np.pi
lambd = random_state.random() * 2 * np.pi

gate = SU2RotationGate(theta, phi, lambd)

expected = gate.rotation_matrix
actual = cirq.unitary(gate)
np.testing.assert_allclose(actual, expected)
52 changes: 1 addition & 51 deletions qualtran/bloqs/generalized_qsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,7 @@
from numpy.typing import NDArray

from qualtran import GateWithRegisters, QBit, Register, Signature


@frozen
class SU2RotationGate(GateWithRegisters):
theta: float
phi: float
lambd: float

@cached_property
def signature(self) -> Signature:
return Signature.build(q=1)

@cached_property
def rotation_matrix(self):
r"""Implements an arbitrary SU(2) rotation.
The rotation is represented by the matrix:
$$
\begin{matrix}
e^{i(\lambda + \phi)} \cos(\theta) & e^{i\phi} \sin(\theta) \\
e^{i\lambda} \sin(\theta) & - \cos(\theta)
\end{matrix}
$$
Returns:
A 2x2 rotation matrix
References:
[Generalized Quantum Signal Processing](https://arxiv.org/abs/2308.01501)
Motlagh and Wiebe. (2023). Equation 7.
"""
return np.array(
[
[
np.exp(1j * (self.lambd + self.phi)) * np.cos(self.theta),
np.exp(1j * self.phi) * np.sin(self.theta),
],
[np.exp(1j * self.lambd) * np.sin(self.theta), -np.cos(self.theta)],
]
)

def decompose_from_registers(
self, *, context: cirq.DecompositionContext, q: NDArray[cirq.Qid]
) -> cirq.OP_TREE:
qubit = q[0]

yield cirq.Rz(rads=np.pi - self.lambd).on(qubit)
yield cirq.Ry(rads=2 * self.theta).on(qubit)
yield cirq.Rz(rads=-self.phi).on(qubit)
yield cirq.GlobalPhaseGate(np.exp(1j * (np.pi + self.lambd + self.phi) / 2)).on()
from qualtran.bloqs.basic_gates.su2_rotation import SU2RotationGate


def qsp_complementary_polynomial(
Expand Down
17 changes: 1 addition & 16 deletions qualtran/bloqs/generalized_qsp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
from numpy.typing import NDArray

from qualtran import GateWithRegisters, Signature
from qualtran.bloqs.basic_gates.su2_rotation import SU2RotationGate
from qualtran.bloqs.generalized_qsp import (
GeneralizedQSP,
qsp_complementary_polynomial,
qsp_phase_factors,
SU2RotationGate,
)


Expand All @@ -39,21 +39,6 @@ def assert_angles_almost_equal(
np.testing.assert_almost_equal(np.exp(np.array(actual) * 1j), np.exp(np.array(desired) * 1j))


def test_cirq_decompose_SU2_to_single_qubit_pauli_gates():
random_state = np.random.default_rng(42)

for _ in range(20):
theta = random_state.random() * 2 * np.pi
phi = random_state.random() * 2 * np.pi
lambd = random_state.random() * 2 * np.pi

gate = SU2RotationGate(theta, phi, lambd)

expected = gate.rotation_matrix
actual = cirq.unitary(gate)
np.testing.assert_allclose(actual, expected)


def check_polynomial_pair_on_random_points_on_unit_circle(
P: Union[Sequence[complex], Polynomial],
Q: Union[Sequence[complex], Polynomial],
Expand Down

0 comments on commit ce193f2

Please sign in to comment.