def test_decompose_returns_deep_op_tree(): class DummyGate(cirq.Gate, cirq.CompositeGate): def default_decompose(self, qubits): q0, q1 = qubits # Yield a tuple yield ((X(q0), Y(q0)), Z(q0)) # Yield nested lists yield [X(q0), [Y(q0), Z(q0)]] def generator(depth): if depth <= 0: yield CZ(q0, q1), Y(q0) else: yield X(q0), generator(depth - 1) yield Z(q0) # Yield nested generators yield generator(2) q0, q1 = QubitId(), QubitId() circuit = cirq.Circuit.from_ops(DummyGate()(q0, q1)) opt = cirq.ExpandComposite() opt.optimize_circuit(circuit) expected = cirq.Circuit().from_ops(X(q0), Y(q0), Z(q0), # From tuple X(q0), Y(q0), Z(q0), # From nested lists # From nested generators X(q0), X(q0), CZ(q0, q1), Y(q0), Z(q0), Z(q0)) assert_equal_mod_empty(expected, circuit)
def default_decompose(self, qubits): c, t = qubits yield Z(c) yield Y(t)**-0.5 yield CZ(c, t) yield Y(t)**0.5 yield Z(c)
def test_composite_default(): q0, q1 = QubitId(), QubitId() cnot = CNOT(q0, q1) circuit = cirq.Circuit() circuit.append(cnot) opt = cirq.ExpandComposite() opt.optimize_circuit(circuit) expected = cirq.Circuit() expected.append([Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5]) assert_equal_mod_empty(expected, circuit)
def test_multiple_composite_default(): q0, q1 = QubitId(), QubitId() cnot = CNOT(q0, q1) circuit = Circuit() circuit.append([cnot, cnot]) opt = ExpandComposite() opt.optimize_circuit(circuit) expected = Circuit() decomp = [Y(q1)**-0.5, CZ(q0, q1), Y(q1)**0.5] expected.append([decomp, decomp]) assert_equal_mod_empty(expected, circuit)
def test_composite_extension_overrides(): q0, q1 = QubitId(), QubitId() cnot = CNOT(q0, q1) circuit = cirq.Circuit() circuit.append(cnot) ext = cirq.Extensions() ext.add_cast(cirq.CompositeGate, cirq.CNotGate, lambda _: OtherCNot()) opt = cirq.ExpandComposite(composite_gate_extension=ext) opt.optimize_circuit(circuit) expected = cirq.Circuit() expected.append([Z(q0), Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5, Z(q0)]) assert_equal_mod_empty(expected, circuit)
def test_mix_composite_non_composite(): q0, q1 = QubitId(), QubitId() actual = cirq.Circuit.from_ops(X(q0), CNOT(q0, q1), X(q1)) opt = cirq.ExpandComposite() opt.optimize_circuit(actual) expected = cirq.Circuit.from_ops(X(q0), Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5, X(q1), strategy=cirq.InsertStrategy.NEW) assert_equal_mod_empty(expected, actual)
def default_decompose(self, qubits): q0, q1 = qubits # Yield a tuple yield ((X(q0), Y(q0)), Z(q0)) # Yield nested lists yield [X(q0), [Y(q0), Z(q0)]] def generator(depth): if depth <= 0: yield CZ(q0, q1), Y(q0) else: yield X(q0), generator(depth - 1) yield Z(q0) # Yield nested generators yield generator(2)
def test_ignore_non_composite(): q0, q1 = QubitId(), QubitId() circuit = cirq.Circuit() circuit.append([X(q0), Y(q1), CZ(q0, q1), Z(q0)]) expected = circuit.copy() opt = cirq.ExpandComposite() opt.optimize_circuit(circuit) assert_equal_mod_empty(expected, circuit)
def test_circuit_from_quil(): q0, q1, q2 = LineQubit.range(3) cirq_circuit = Circuit([ I(q0), I(q1), I(q2), X(q0), Y(q1), Z(q2), H(q0), S(q1), T(q2), Z(q0)**(1 / 8), Z(q1)**(1 / 8), Z(q2)**(1 / 8), rx(np.pi / 2)(q0), ry(np.pi / 2)(q1), rz(np.pi / 2)(q2), CZ(q0, q1), CNOT(q1, q2), cphase(np.pi / 2)(q0, q1), cphase00(np.pi / 2)(q1, q2), cphase01(np.pi / 2)(q0, q1), cphase10(np.pi / 2)(q1, q2), ISWAP(q0, q1), pswap(np.pi / 2)(q1, q2), SWAP(q0, q1), xy(np.pi / 2)(q1, q2), CCNOT(q0, q1, q2), CSWAP(q0, q1, q2), MeasurementGate(1, key="ro[0]")(q0), MeasurementGate(1, key="ro[1]")(q1), MeasurementGate(1, key="ro[2]")(q2), ]) # build the same Circuit, using Quil quil_circuit = circuit_from_quil(QUIL_PROGRAM) # test Circuit equivalence assert cirq_circuit == quil_circuit pyquil_circuit = Program(QUIL_PROGRAM) # drop declare and measures, get Program unitary pyquil_unitary = program_unitary(pyquil_circuit[1:-3], n_qubits=3) # fix qubit order convention cirq_circuit_swapped = Circuit(SWAP(q0, q2), cirq_circuit[:-1], SWAP(q0, q2)) # get Circuit unitary cirq_unitary = cirq_circuit_swapped.unitary() # test unitary equivalence assert np.isclose(pyquil_unitary, cirq_unitary).all()
def test_recursive_composite_extension_overrides(): q0, q1 = QubitId(), QubitId() swap = SWAP(q0, q1) circuit = cirq.Circuit() circuit.append(swap) ext = cirq.Extensions() ext.add_cast(cirq.CompositeGate, cirq.CNotGate, lambda _: OtherCNot()) opt = cirq.ExpandComposite(composite_gate_extension=ext) opt.optimize_circuit(circuit) expected = cirq.Circuit() expected.append([Z(q0), Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5, Z(q0)]) expected.append([Z(q1), Y(q0) ** -0.5, CZ(q1, q0), Y(q0) ** 0.5, Z(q1)], strategy=cirq.InsertStrategy.INLINE) expected.append([Z(q0), Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5, Z(q0)], strategy=cirq.InsertStrategy.INLINE) assert_equal_mod_empty(expected, circuit)
def test_recursive_composite(): q0, q1 = QubitId(), QubitId() swap = SWAP(q0, q1) circuit = Circuit() circuit.append(swap) opt = ExpandComposite() opt.optimize_circuit(circuit) expected = Circuit().from_ops( Y(q1)**-0.5, CZ(q0, q1), Y(q1)**0.5, Y(q0)**-0.5, CZ(q1, q0), Y(q0)**0.5, Y(q1)**-0.5, CZ(q0, q1), Y(q1)**0.5) assert_equal_mod_empty(expected, circuit)
def generator(depth): if depth <= 0: yield CZ(q0, q1), Y(q0) else: yield X(q0), generator(depth - 1) yield Z(q0)