Skip to content

Commit 3381f30

Browse files
committed
small unit tests
1 parent e1aaeee commit 3381f30

7 files changed

Lines changed: 97 additions & 1 deletion

File tree

qualtran/bloqs/arithmetic/addition_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,8 @@ def test_controlled_add_from_add():
449449
assert ctrl == 0
450450
assert a == 5
451451
assert b == 15
452+
453+
454+
def test_add_k_str():
455+
add_k = AddK(QUInt(5), k=3)
456+
assert str(add_k) == "AddK(k=3)"

qualtran/bloqs/arithmetic/bitwise_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,8 @@ def test_bitwisenot_classical_action(dtype, bitsize):
184184
else:
185185
valid_range = range(2**bitsize)
186186
qlt_testing.assert_consistent_classical_action(b, x=valid_range)
187+
188+
189+
def test_bitwise_not_str():
190+
bloq = BitwiseNot(QUInt(5))
191+
assert str(bloq) == "BitwiseNot(5)"

qualtran/bloqs/basic_gates/global_phase_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,8 @@ def test_t_complexity():
7777

7878
def test_global_phase(bloq_autotester):
7979
bloq_autotester(_global_phase)
80+
81+
82+
def test_global_phase_str():
83+
bloq = GlobalPhase(exponent=0.5)
84+
assert str(bloq) == "GPhase(0.5)"

qualtran/bloqs/basic_gates/on_each_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ def test_call_graph():
5959
c1 = x_on_each.bloq_counts(generalizer=ignore_split_join)
6060
c2 = x_on_each.decompose_bloq().bloq_counts(generalizer=ignore_split_join)
6161
assert c1 == c2
62+
63+
64+
def test_on_each_str():
65+
on_each = OnEach(10, XGate())
66+
assert str(on_each) == "X(oneach=10)"

qualtran/bloqs/basic_gates/power_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,42 @@ def to_cirq_circuit(bloq: GateWithRegisters) -> cirq.Circuit:
9090
@pytest.mark.slow
9191
def test_no_circular_import():
9292
subprocess.check_call(['python', '-c', 'from qualtran.bloqs.basic_gates import power'])
93+
94+
95+
def test_power_wire_symbol():
96+
from qualtran import Signature
97+
from qualtran.drawing import LarrowTextBox, RarrowTextBox, Text, TextBox
98+
99+
class MockBloq(GateWithRegisters):
100+
@property
101+
def signature(self):
102+
return Signature([])
103+
104+
def wire_symbol(self, reg, idx=tuple()):
105+
if reg is None:
106+
return Text("Mock")
107+
if reg.name == 'a':
108+
return Text("A")
109+
if reg.name == 'b':
110+
return TextBox("B")
111+
if reg.name == 'c':
112+
return LarrowTextBox("C")
113+
if reg.name == 'd':
114+
return RarrowTextBox("D")
115+
116+
bloq = MockBloq()
117+
power_bloq = Power(bloq, 3)
118+
119+
assert power_bloq.wire_symbol(None) == Text("Mock**3")
120+
121+
from qualtran import QUInt, Register
122+
123+
reg_a = Register('a', QUInt(1))
124+
reg_b = Register('b', QUInt(1))
125+
reg_c = Register('c', QUInt(1))
126+
reg_d = Register('d', QUInt(1))
127+
128+
assert power_bloq.wire_symbol(reg_a) == Text("A**3")
129+
assert power_bloq.wire_symbol(reg_b) == TextBox("B**3")
130+
assert power_bloq.wire_symbol(reg_c) == LarrowTextBox("C**3")
131+
assert power_bloq.wire_symbol(reg_d) == RarrowTextBox("D**3")

qualtran/bloqs/basic_gates/rotation_test.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
ZGate,
3434
ZPowGate,
3535
)
36-
from qualtran.bloqs.basic_gates.rotation import _crz, _cz_pow, _rx, _ry, _rz, _z_pow, CRz
36+
from qualtran.bloqs.basic_gates.rotation import _crz, _cz_pow, _rx, _ry, _rz, _z_pow, CRy, CRz
3737
from qualtran.resource_counting import GateCounts, get_cost_value, QECGatesCost
3838
from qualtran.resource_counting.classify_bloqs import bloq_is_rotation, bloq_is_t_like
3939

@@ -156,6 +156,7 @@ def test_t_like_rotation_gates():
156156
assert not bloq_is_rotation(Rx(angle))
157157
assert not bloq_is_rotation(Ry(angle))
158158
assert not bloq_is_rotation(Rz(angle))
159+
assert not bloq_is_rotation(CRy(angle))
159160
assert bloq_is_t_like(Rx(angle))
160161
assert bloq_is_t_like(Ry(angle))
161162
assert bloq_is_t_like(Rz(angle))
@@ -267,6 +268,8 @@ def test_str():
267268
assert str(CZPowGate(1.0)) == 'CZ(pow=1.0)'
268269
assert str(CZPowGate(0.9)) == 'CZ(pow=0.9)'
269270

271+
assert str(CRz(1.0)) == 'CRz(1.0)'
272+
270273

271274
def test_rx(bloq_autotester):
272275
bloq_autotester(_rx)
@@ -278,3 +281,27 @@ def test_ry(bloq_autotester):
278281

279282
def test_rz(bloq_autotester):
280283
bloq_autotester(_rz)
284+
285+
286+
def test_cry():
287+
from qualtran.bloqs.basic_gates.rotation import CRy
288+
289+
rs = np.random.RandomState(52)
290+
angle = rs.uniform(0, 2 * np.pi)
291+
292+
u1 = CRy(angle=angle).tensor_contract()
293+
u2 = cirq.unitary(cirq.Ry(rads=angle).controlled())
294+
u3 = Controlled(Ry(angle=angle), CtrlSpec()).tensor_contract()
295+
np.testing.assert_allclose(u1, u2, atol=1e-8)
296+
np.testing.assert_allclose(u1, u3, atol=1e-8)
297+
298+
ry = Ry(angle=angle)
299+
assert ry.controlled() == CRy(angle=angle)
300+
301+
ctrl_bloq = Controlled(ry.as_composite_bloq(), CtrlSpec()).decompose_bloq()
302+
(cry_inst,) = list(ctrl_bloq.bloq_instances)
303+
assert cry_inst.bloq == CRy(angle=angle)
304+
305+
# testing the specialized ctrl
306+
ctrl, bloq_with_ctrl = ry.get_ctrl_system(CtrlSpec())
307+
assert ctrl == CRy(angle=angle)

qualtran/bloqs/mcmt/multi_control_pauli_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,13 @@ def test_classical_multi_control_x(cvs, x, ctrls, result):
114114
@pytest.mark.notebook
115115
def test_notebook():
116116
qlt_testing.execute_notebook('multi_control_multi_target_pauli')
117+
118+
119+
def test_multi_control_pauli_str():
120+
from qualtran.bloqs.mcmt.multi_control_pauli import MultiControlX, MultiControlZ
121+
122+
mcx = MultiControlX(cvs=(1, 0, 1))
123+
assert str(mcx) == "MultiControlX(3)"
124+
125+
mcz = MultiControlZ(cvs=(1, 1))
126+
assert str(mcz) == "MultiControlZ(2)"

0 commit comments

Comments
 (0)