def test_repeat_limit(): q0, q1, q2 = cirq.LineQubit.range(3) c_orig = cirq.testing.nonoptimal_toffoli_circuit(q0, q1, q2) c_opt = optimized_circuit(c_orig, repeat=1) cirq.testing.assert_allclose_up_to_global_phase( c_orig.to_unitary_matrix(), c_opt.to_unitary_matrix(), atol=1e-7, ) assert sum(1 for op in c_opt.all_operations() if isinstance( op, cirq.GateOperation) and isinstance(op.gate, cirq.CZPowGate)) >= 10
def test_optimize(): q0, q1, q2 = cirq.LineQubit.range(3) c_orig = cirq.Circuit.from_ops( cirq.X(q0)**0.5, cirq.X(q1), cirq.CZ(q1, q2), cirq.X(q2)**0.125, cirq.Z(q1)**0.5, cirq.Y(q1)**0.5, cirq.CZ(q0, q1), cirq.Z(q1)**0.5, cirq.CZ(q1, q2), cirq.Z(q1)**0.5, cirq.X(q2)**0.875, cirq.CZ(q1, q2), cirq.X(q2)**0.125, ) cirq.testing.assert_has_diagram( c_orig, """ 0: ───X^0.5─────────────────────────@─────────────────────────────────── │ 1: ───X───────@───S─────────Y^0.5───@───S───@───S─────────@───────────── │ │ │ 2: ───────────@───X^0.125───────────────────@───X^0.875───@───X^0.125─── """) c_opt = optimized_circuit(c_orig) cirq.testing.assert_allclose_up_to_global_phase( c_orig.to_unitary_matrix(), c_opt.to_unitary_matrix(), atol=1e-7, ) cirq.testing.assert_has_diagram( c_opt, """ 0: ───X^0.5────────────@──────────────────────────────────────── │ 1: ───@───────X^-0.5───@───@────────────────@───Z^-0.5────────── │ │ │ 2: ───@────────────────────@───[X]^-0.875───@───[X]^-0.25───Z─── """)