Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
austingmhuang committed Mar 3, 2025
1 parent 4eb78a2 commit 2afc614
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 5 deletions.
13 changes: 13 additions & 0 deletions qualtran/bloqs/basic_gates/cnot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import cirq
import numpy as np
import pytest
import pennylane as qml

from qualtran import BloqBuilder, Signature
from qualtran.bloqs.basic_gates import CNOT, PlusState, ZeroState
Expand All @@ -38,6 +39,18 @@ def test_cnot_tensor():
# fmt: on
np.testing.assert_allclose(should_be, matrix)

def test_cnot_vs_pl():
bloq = CNOT()
matrix = qml.FromBloq(bloq, wires=[0, 1]).matrix()
# fmt: off
should_be = np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0]])
# fmt: on
np.testing.assert_allclose(should_be, matrix)


def test_cnot_cbloq_tensor_vs_cirq():
bb, soqs = BloqBuilder.from_signature(Signature.build(c=1, t=1))
Expand Down
3 changes: 2 additions & 1 deletion qualtran/bloqs/basic_gates/global_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ def cirq_gate(self) -> cirq.Gate:

def as_pl_op(self, wires: 'Wires') -> 'Operation':
import pennylane as qml
import numpy as np

return qml.GlobalPhase(phi=self.coefficient, wires=wires)
return qml.GlobalPhase(phi=self.exponent * np.pi, wires=wires)

def decompose_bloq(self) -> 'CompositeBloq':
raise DecomposeTypeError(f"{self} is atomic")
Expand Down
7 changes: 7 additions & 0 deletions qualtran/bloqs/basic_gates/global_phase_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import cirq
import numpy as np
import pytest
import pennylane as qml

from qualtran import CtrlSpec
from qualtran.bloqs.basic_gates.global_phase import _global_phase, GlobalPhase
Expand Down Expand Up @@ -53,6 +54,12 @@ def test_cirq_interop():

assert cirq_gate_to_bloq(gate) == bloq

def test_pl_interop():
bloq = GlobalPhase(exponent=0.5)
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.GlobalPhase(phi=0.5*np.pi, wires=[0])
assert pl_op_from_bloq == pl_op


def test_t_complexity():
assert GlobalPhase(exponent=0.5).t_complexity() == TComplexity()
Expand Down
7 changes: 7 additions & 0 deletions qualtran/bloqs/basic_gates/hadamard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def test_cirq_interop():
assert op.gate is not None
assert cirq_gate_to_bloq(op.gate) == CHadamard()

def test_pl_interop():
import pennylane as qml
bloq = Hadamard()
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.Hadamard(wires=[0])
assert pl_op_from_bloq == pl_op


def test_active_chadamard_is_hadamard():
bb = BloqBuilder()
Expand Down
7 changes: 7 additions & 0 deletions qualtran/bloqs/basic_gates/identity_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def test_to_cirq():
vec2 = cirq.final_state_vector(circuit)
np.testing.assert_allclose(vec1, vec2)

def test_pl_interop():
import pennylane as qml
bloq = Identity()
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.Identity(wires=[0])
assert pl_op_from_bloq == pl_op


def test_to_cirq_n_qubit_id():
circuit = Identity(3).as_composite_bloq().to_cirq_circuit()
Expand Down
21 changes: 21 additions & 0 deletions qualtran/bloqs/basic_gates/rotation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,27 @@ def test_as_cirq_op():
circuit = cirq.Circuit(op)
assert circuit == cirq.Circuit(cirq.ZPowGate(exponent=1 / 5).on(cirq.NamedQubit("q")))

def test_pl_interop():
import pennylane as qml
bloq = Rx(angle=0.4)
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.RX(phi=0.4, wires=[0])
assert pl_op_from_bloq == pl_op

bloq = Ry(angle=0.4)
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.RY(phi=0.4, wires=[0])
assert pl_op_from_bloq == pl_op

bloq = Rz(angle=0.4)
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.RZ(phi=0.4, wires=[0])
assert pl_op_from_bloq == pl_op

bloq = Rx(angle=0.4)
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.RX(phi=0.4, wires=[0])
assert pl_op_from_bloq == pl_op

def test_str():
assert str(ZPowGate()) == "Z**1.0"
Expand Down
5 changes: 3 additions & 2 deletions qualtran/bloqs/basic_gates/s_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ def as_cirq_op(
def as_pl_op(self, wires: 'Wires') -> 'Operation':
import pennylane as qml

p = -1 if self.is_adjoint else 1
if self.is_adjoint:
return qml.S(wires=wires).adjoint()

return qml.S(wires=wires) ** p
return qml.S(wires=wires)

def __str__(self) -> str:
maybe_dag = '†' if self.is_adjoint else ''
Expand Down
6 changes: 6 additions & 0 deletions qualtran/bloqs/basic_gates/s_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def test_to_cirq():
circuit = cbloq.to_cirq_circuit()
cirq.testing.assert_has_diagram(circuit, "_c(0): ───H───S───S^-1───")

def test_pl_interop():
import pennylane as qml
bloq = SGate()
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.S(wires=[0])
assert pl_op_from_bloq == pl_op

def test_tensors():
from_cirq = cirq.unitary(cirq.Circuit(cirq.S(cirq.LineQubit(0))))
Expand Down
11 changes: 11 additions & 0 deletions qualtran/bloqs/basic_gates/swap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ def test_two_bit_swap_as_cirq_op():
)
cirq.testing.assert_same_circuits(expected_circuit, cbloq_to_circuit)

def test_pl_interop():
import pennylane as qml
bloq = TwoBitSwap()
pl_op_from_bloq = bloq.as_pl_op(wires=[0, 1])
pl_op = qml.SWAP(wires=[0, 1])
assert pl_op_from_bloq == pl_op

bloq = TwoBitCSwap()
pl_op_from_bloq = bloq.as_pl_op(wires=[0, 1, 2])
pl_op = qml.CSWAP(wires=[0, 1, 2])
assert pl_op_from_bloq == pl_op

def test_two_bit_swap_call_classically():
swap = TwoBitSwap()
Expand Down
6 changes: 4 additions & 2 deletions qualtran/bloqs/basic_gates/t_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ def as_cirq_op(
def as_pl_op(self, wires: 'Wires') -> 'Operation':
import pennylane as qml

p = -1 if self.is_adjoint else 1
return qml.T(wires=wires) ** p
if self.is_adjoint:
return qml.T(wires=wires).adjoint()

return qml.T(wires=wires)

def __str__(self):
maybe_dag = '†' if self.is_adjoint else ''
Expand Down
7 changes: 7 additions & 0 deletions qualtran/bloqs/basic_gates/t_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ def test_to_cirq():
circuit = cbloq.to_cirq_circuit()
cirq.testing.assert_has_diagram(circuit, "_c(0): ───H───T───T^-1───")

def test_pl_interop():
import pennylane as qml
bloq = TGate()
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.T(wires=[0])
assert pl_op_from_bloq == pl_op


def test_tensors():
from_cirq = cirq.unitary(cirq.Circuit(cirq.T(cirq.LineQubit(0))))
Expand Down
7 changes: 7 additions & 0 deletions qualtran/bloqs/basic_gates/toffoli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def test_toffoli_cirq():
Toffoli(), [Circle(filled=True), Circle(filled=True), ModPlus()]
)

def test_pl_interop():
import pennylane as qml
bloq = Toffoli()
pl_op_from_bloq = bloq.as_pl_op(wires=[0, 1, 2])
pl_op = qml.Toffoli(wires=[0, 1, 2])
assert pl_op_from_bloq == pl_op


def test_classical_sim():
tof = Toffoli()
Expand Down
7 changes: 7 additions & 0 deletions qualtran/bloqs/basic_gates/x_basis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ def test_to_cirq():
vec2 = cirq.final_state_vector(circuit)
np.testing.assert_allclose(vec1, vec2)

def test_pl_interop():
import pennylane as qml
bloq = XGate()
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.X(wires=[0])
assert pl_op_from_bloq == pl_op


def test_x_truth_table():
classical_truth_table = format_classical_truth_table(*get_classical_truth_table(XGate()))
Expand Down
7 changes: 7 additions & 0 deletions qualtran/bloqs/basic_gates/y_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ def test_to_cirq():
vec2 = cirq.final_state_vector(circuit)
np.testing.assert_allclose(vec1, vec2)

def test_pl_interop():
import pennylane as qml
bloq = YGate()
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.Y(wires=[0])
assert pl_op_from_bloq == pl_op


def test_cy_vs_cirq():
bloq = YGate().controlled()
Expand Down
7 changes: 7 additions & 0 deletions qualtran/bloqs/basic_gates/z_basis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ def test_to_cirq():
vec2 = cirq.final_state_vector(circuit)
np.testing.assert_allclose(vec1, vec2)

def test_pl_interop():
import pennylane as qml
bloq = ZGate()
pl_op_from_bloq = bloq.as_pl_op(wires=[0])
pl_op = qml.Z(wires=[0])
assert pl_op_from_bloq == pl_op


def test_zgate_manual():
z = ZGate()
Expand Down

0 comments on commit 2afc614

Please sign in to comment.