def test_optimize_for_target_gateset(): q = cirq.LineQubit.range(4) c_orig = cirq.Circuit( cirq.QuantumFourierTransformGate(4).on(*q), cirq.Y(q[0]).with_tags("ignore"), cirq.Y(q[1]).with_tags("ignore"), cirq.CNOT(*q[2:]).with_tags("ignore"), cirq.measure(*q[:2], key="m"), cirq.CZ(*q[2:]).with_classical_controls("m"), cirq.inverse(cirq.QuantumFourierTransformGate(4).on(*q)), ) cirq.testing.assert_has_diagram( c_orig, ''' 0: ───qft───Y['ignore']───M───────qft^-1─── │ ║ │ 1: ───#2────Y['ignore']───M───────#2─────── │ ║ │ 2: ───#3────@['ignore']───╫───@───#3─────── │ │ ║ ║ │ 3: ───#4────X─────────────╫───@───#4─────── ║ ║ m: ═══════════════════════@═══^════════════ ''', ) gateset = MatrixGateTargetGateset() context = cirq.TransformerContext(tags_to_ignore=("ignore", )) c_new = cirq.optimize_for_target_gateset(c_orig, gateset=gateset, context=context) cirq.testing.assert_has_diagram( c_new, ''' ┌────────┐ ┌────────┐ ┌────────┐ 0: ───M[1]──────────M[1]──────────────────────M[1]────Y['ignore']───M────────M[1]───────────────────────────M[1]────M[1]───M[1]─── │ │ │ ║ │ │ │ │ 1: ───M[2]───M[1]───┼─────────────M[1]────M[1]┼───────Y['ignore']───M────────┼───M[1]───────────M[1]────M[1]┼───────┼──────M[2]─── │ │ │ │ │ ║ │ │ │ │ │ │ 2: ──────────M[2]───M[2]───M[1]───┼───────M[2]┼───────@['ignore']───╫───@────┼───M[2]────M[1]───┼───────M[2]┼───────M[2]────────── │ │ │ │ ║ ║ │ │ │ │ 3: ────────────────────────M[2]───M[2]────────M[2]────X─────────────╫───@────M[2]────────M[2]───M[2]────────M[2]────────────────── ║ ║ m: ═════════════════════════════════════════════════════════════════@═══^═════════════════════════════════════════════════════════ └────────┘ └────────┘ └────────┘ ''', ) with pytest.raises(ValueError, match="Unable to convert"): # Raises an error due to CCO and Measurement gate, which are not part of the gateset. _ = cirq.optimize_for_target_gateset(c_orig, gateset=gateset, context=context, ignore_failures=False)
def generate_all_frequency_space_cell_makers() -> Iterator[CellMaker]: # Frequency space. yield from _family("QFT", lambda n: cirq.QuantumFourierTransformGate(n)) yield from _family( "QFT†", lambda n: cirq.inverse(cirq.QuantumFourierTransformGate(n))) yield from _family( "PhaseGradient", lambda n: cirq.PhaseGradientGate(num_qubits=n, exponent=0.5)) yield from _family( "PhaseUngradient", lambda n: cirq.PhaseGradientGate(num_qubits=n, exponent=-0.5)) yield from _family( "grad^t", lambda n: cirq.PhaseGradientGate( num_qubits=n, exponent=2**(n - 1) * sympy.Symbol('t'))) yield from _family( "grad^-t", lambda n: cirq.PhaseGradientGate( num_qubits=n, exponent=-2**(n - 1) * sympy.Symbol('t')))
def test_qft(): np.testing.assert_allclose( cirq.unitary(cirq.qft(*cirq.LineQubit.range(2))), np.array( [ [1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j], ] ) / 2, atol=1e-8, ) np.testing.assert_allclose( cirq.unitary(cirq.qft(*cirq.LineQubit.range(2), without_reverse=True)), np.array( [ [1, 1, 1, 1], [1, -1, 1, -1], [1, 1j, -1, -1j], [1, -1j, -1, 1j], ] ) / 2, atol=1e-8, ) np.testing.assert_allclose( cirq.unitary(cirq.qft(*cirq.LineQubit.range(4))), np.array([[np.exp(2j * np.pi * i * j / 16) for i in range(16)] for j in range(16)]) / 4, atol=1e-8, ) np.testing.assert_allclose( cirq.unitary(cirq.qft(*cirq.LineQubit.range(2)) ** -1), np.array( [ [1, 1, 1, 1], [1, -1j, -1, 1j], [1, -1, 1, -1], [1, 1j, -1, -1j], ] ) / 2, atol=1e-8, ) for k in range(4): for b in [False, True]: cirq.testing.assert_implements_consistent_protocols( cirq.QuantumFourierTransformGate(num_qubits=k, without_reverse=b) )
def test_cirq_irreconcilable_gate(self): a, b, c, d = [ cirq.GridQubit(0, 0), cirq.GridQubit(0, 1), cirq.GridQubit(1, 1), cirq.GridQubit(1, 0) ] # The QFT gate does not decompose cleanly into the qsim gateset. cirq_circuit = cirq.Circuit( cirq.QuantumFourierTransformGate(4).on(a, b, c, d)) qsimSim = qsimcirq.QSimSimulator() with self.assertRaises(ValueError): qsimSim.simulate(cirq_circuit)
def test_complicated_decomposition(): qubits = cirq.LineQubit.range(4) # The QFT gate decomposes cleanly into the qsim gateset. cirq_circuit = cirq.Circuit(cirq.QuantumFourierTransformGate(4).on(*qubits)) qsimSim = qsimcirq.QSimSimulator() result = qsimSim.simulate(cirq_circuit, qubit_order=qubits) assert result.state_vector().shape == (16,) cirqSim = cirq.Simulator() cirq_result = cirqSim.simulate(cirq_circuit, qubit_order=qubits) # Decomposition may result in gates which add a global phase. assert cirq.linalg.allclose_up_to_global_phase( result.state_vector(), cirq_result.state_vector() )
cirq.X(Q0) * cirq.Y(Q1) * 123 ], 'PhaseDampingChannel': cirq.PhaseDampingChannel(0.5), 'PhaseFlipChannel': cirq.PhaseFlipChannel(0.5), 'PhaseGradientGate': cirq.PhaseGradientGate(num_qubits=3, exponent=0.235), 'PhasedISwapPowGate': cirq.PhasedISwapPowGate(phase_exponent=0.1, exponent=0.2), 'PhasedXPowGate': cirq.PhasedXPowGate(phase_exponent=0.123, exponent=0.456, global_shift=0.789), 'QuantumFourierTransformGate': cirq.QuantumFourierTransformGate(num_qubits=2, without_reverse=True), 'ResetChannel': cirq.ResetChannel(), 'X': cirq.X, 'Y': cirq.Y, 'Z': cirq.Z, 'S': cirq.S, 'SWAP': cirq.SWAP, 'SingleQubitPauliStringGateOperation': cirq.X(Q0), 'SwapPowGate': [cirq.SwapPowGate(), cirq.SWAP**0.5],
def test_quantum_fourier_transform_gate_repr(): b = cirq.QuantumFourierTransformGate(num_qubits=2, without_reverse=False) cirq.testing.assert_equivalent_repr(b)
q0, q1 = cirq.LineQubit.range(2) for _ in range(10): gate = cirq.MatrixGate(cirq.testing.random_unitary(4)) circuit = cirq.Circuit(gate(q0, q1)) decomposed_circuit = cirq.optimize_for_target_gateset( circuit, gateset=ionq_target_gateset, ignore_failures=False) cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent( circuit, decomposed_circuit, atol=1e-8) assert VALID_DECOMPOSED_GATES.validate(decomposed_circuit) @pytest.mark.parametrize( 'gate, qubits', [ (cirq.CCZ, 3), (cirq.QuantumFourierTransformGate(6), 6), (cirq.MatrixGate(cirq.testing.random_unitary(8)), 3), ], ) def test_decompose_multi_qubit_cirq_gates(gate, qubits): circuit = cirq.Circuit(gate(*cirq.LineQubit.range(qubits))) decomposed_circuit = cirq.optimize_for_target_gateset( circuit, gateset=ionq_target_gateset, ignore_failures=False) cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent( circuit, decomposed_circuit, atol=1e-8) assert ionq_target_gateset.validate(decomposed_circuit) def test_decompose_parameterized_operation(): op = cirq.ISWAP(*cirq.LineQubit.range(2)) theta = sympy.Symbol("theta")