def cirqswapgate( circuit: cirq.Circuit, qubit1: cirq.GridQubit, qubit2: cirq.GridQubit, symbol: Symbol, ) -> cirq.Circuit: circuit.append(cirq.SwapPowGate(exponent=symbol)(qubit1, qubit2)) return circuit
def test_get_qcircuit_diagram_info(): qubits = cirq.NamedQubit('x'), cirq.NamedQubit('y') gate = cirq.SwapPowGate(exponent=0.5) op = gate(*qubits) qubit_map = {q: i for i, q in enumerate(qubits)} args = cirq.CircuitDiagramInfoArgs( known_qubits=qubits, known_qubit_count=None, use_unicode_characters=True, precision=3, label_map=qubit_map, ) actual_info = ccq.get_qcircuit_diagram_info(op, args) name = r'{\text{SWAP}^{0.5}}' expected_info = cirq.CircuitDiagramInfo( (r'\multigate{1}' + name, r'\ghost' + name), exponent=1, connected=False) assert actual_info == expected_info gate = cirq.SWAP op = gate(*qubits) qubit_map = {q: i for q, i in zip(qubits, (4, 3))} args = cirq.CircuitDiagramInfoArgs( known_qubits=qubits, known_qubit_count=None, use_unicode_characters=True, precision=3, label_map=qubit_map, ) actual_info = ccq.get_qcircuit_diagram_info(op, args) expected_info = cirq.CircuitDiagramInfo( (r'\ghost{\text{SWAP}}', r'\multigate{1}{\text{SWAP}}'), connected=False) assert actual_info == expected_info qubit_map = {q: i for q, i in zip(qubits, (2, 5))} args = cirq.CircuitDiagramInfoArgs( known_qubits=qubits, known_qubit_count=None, use_unicode_characters=True, precision=3, label_map=qubit_map, ) actual_info = ccq.get_qcircuit_diagram_info(op, args) expected_info = cirq.CircuitDiagramInfo((r'\gate{\text{Swap}}', ) * 2) assert actual_info == expected_info actual_info = ccq.get_qcircuit_diagram_info( op, cirq.CircuitDiagramInfoArgs.UNINFORMED_DEFAULT) assert actual_info == expected_info
def test_text_to_qcircuit_diagrammable(): qubits = cirq.NamedQubit('x'), cirq.NamedQubit('y') g = cirq.SwapPowGate(exponent=0.5) f = _TextToQCircuitDiagrammable(g) qubit_map = {q: i for i, q in enumerate(qubits)} actual_info = f.qcircuit_diagram_info( cirq.CircuitDiagramInfoArgs(known_qubits=qubits, known_qubit_count=None, use_unicode_characters=True, precision=3, qubit_map=qubit_map)) name = '{\\text{SWAP}^{0.5}}' expected_info = cirq.CircuitDiagramInfo( ('\multigate{1}' + name, '\ghost' + name), exponent=0.5, connected=False) assert actual_info == expected_info g = cirq.SWAP f = _TextToQCircuitDiagrammable(g) qubit_map = {q: i for q, i in zip(qubits, (4, 3))} actual_info = f.qcircuit_diagram_info( cirq.CircuitDiagramInfoArgs(known_qubits=qubits, known_qubit_count=None, use_unicode_characters=True, precision=3, qubit_map=qubit_map)) expected_info = cirq.CircuitDiagramInfo( ('\ghost{\\text{SWAP}}', '\multigate{1}{\\text{SWAP}}'), connected=False) assert actual_info == expected_info qubit_map = {q: i for q, i in zip(qubits, (2, 5))} actual_info = f.qcircuit_diagram_info( cirq.CircuitDiagramInfoArgs(known_qubits=qubits, known_qubit_count=None, use_unicode_characters=True, precision=3, qubit_map=qubit_map)) expected_info = cirq.CircuitDiagramInfo(('\\gate{\\text{×}}', ) * 2) assert actual_info == expected_info actual_info = f.qcircuit_diagram_info( cirq.CircuitDiagramInfoArgs.UNINFORMED_DEFAULT) assert actual_info == expected_info
def test_cirq_qsim_global_shift(self): q0 = cirq.GridQubit(1, 1) q1 = cirq.GridQubit(1, 0) q2 = cirq.GridQubit(0, 1) q3 = cirq.GridQubit(0, 0) circuit = cirq.Circuit( cirq.Moment([ cirq.H(q0), cirq.H(q1), cirq.H(q2), cirq.H(q3), ]), cirq.Moment([ cirq.CXPowGate(exponent=1, global_shift=0.7)(q0, q1), cirq.CZPowGate(exponent=1, global_shift=0.9)(q2, q3), ]), cirq.Moment([ cirq.XPowGate(exponent=1, global_shift=1.1)(q0), cirq.YPowGate(exponent=1, global_shift=1)(q1), cirq.ZPowGate(exponent=1, global_shift=0.9)(q2), cirq.HPowGate(exponent=1, global_shift=0.8)(q3), ]), cirq.Moment([ cirq.XXPowGate(exponent=1, global_shift=0.2)(q0, q1), cirq.YYPowGate(exponent=1, global_shift=0.3)(q2, q3), ]), cirq.Moment([ cirq.ZPowGate(exponent=0.25, global_shift=0.4)(q0), cirq.ZPowGate(exponent=0.5, global_shift=0.5)(q1), cirq.YPowGate(exponent=1, global_shift=0.2)(q2), cirq.ZPowGate(exponent=1, global_shift=0.3)(q3), ]), cirq.Moment([ cirq.ZZPowGate(exponent=1, global_shift=0.2)(q0, q1), cirq.SwapPowGate(exponent=1, global_shift=0.3)(q2, q3), ]), cirq.Moment([ cirq.XPowGate(exponent=1, global_shift=0)(q0), cirq.YPowGate(exponent=1, global_shift=0)(q1), cirq.ZPowGate(exponent=1, global_shift=0)(q2), cirq.HPowGate(exponent=1, global_shift=0)(q3), ]), cirq.Moment([ cirq.ISwapPowGate(exponent=1, global_shift=0.3)(q0, q1), cirq.ZZPowGate(exponent=1, global_shift=0.5)(q2, q3), ]), cirq.Moment([ cirq.ZPowGate(exponent=0.5, global_shift=0)(q0), cirq.ZPowGate(exponent=0.25, global_shift=0)(q1), cirq.XPowGate(exponent=0.9, global_shift=0)(q2), cirq.YPowGate(exponent=0.8, global_shift=0)(q3), ]), cirq.Moment([ cirq.CZPowGate(exponent=0.3, global_shift=0)(q0, q1), cirq.CXPowGate(exponent=0.4, global_shift=0)(q2, q3), ]), cirq.Moment([ cirq.ZPowGate(exponent=1.3, global_shift=0)(q0), cirq.HPowGate(exponent=0.8, global_shift=0)(q1), cirq.XPowGate(exponent=0.9, global_shift=0)(q2), cirq.YPowGate(exponent=0.4, global_shift=0)(q3), ]), cirq.Moment([ cirq.XXPowGate(exponent=0.8, global_shift=0)(q0, q1), cirq.YYPowGate(exponent=0.6, global_shift=0)(q2, q3), ]), cirq.Moment([ cirq.HPowGate(exponent=0.7, global_shift=0)(q0), cirq.ZPowGate(exponent=0.2, global_shift=0)(q1), cirq.YPowGate(exponent=0.3, global_shift=0)(q2), cirq.XPowGate(exponent=0.7, global_shift=0)(q3), ]), cirq.Moment([ cirq.ZZPowGate(exponent=0.1, global_shift=0)(q0, q1), cirq.SwapPowGate(exponent=0.6, global_shift=0)(q2, q3), ]), cirq.Moment([ cirq.XPowGate(exponent=0.4, global_shift=0)(q0), cirq.YPowGate(exponent=0.3, global_shift=0)(q1), cirq.ZPowGate(exponent=0.2, global_shift=0)(q2), cirq.HPowGate(exponent=0.1, global_shift=0)(q3), ]), cirq.Moment([ cirq.ISwapPowGate(exponent=1.3, global_shift=0)(q0, q1), cirq.CXPowGate(exponent=0.5, global_shift=0)(q2, q3), ]), cirq.Moment([ cirq.H(q0), cirq.H(q1), cirq.H(q2), cirq.H(q3), ]), ) simulator = cirq.Simulator() cirq_result = simulator.simulate(circuit) qsim_simulator = qsimcirq.QSimSimulator() qsim_result = qsim_simulator.simulate(circuit) assert cirq.linalg.allclose_up_to_global_phase( qsim_result.state_vector(), cirq_result.state_vector())
def test_cirq_qsim_all_supported_gates(self): q0 = cirq.GridQubit(1, 1) q1 = cirq.GridQubit(1, 0) q2 = cirq.GridQubit(0, 1) q3 = cirq.GridQubit(0, 0) circuit = cirq.Circuit( cirq.Moment([ cirq.H(q0), cirq.H(q1), cirq.H(q2), cirq.H(q3), ]), cirq.Moment([ cirq.T(q0), cirq.T(q1), cirq.T(q2), cirq.T(q3), ]), cirq.Moment([ cirq.CZPowGate(exponent=0.7, global_shift=0.2)(q0, q1), cirq.CXPowGate(exponent=1.2, global_shift=0.4)(q2, q3), ]), cirq.Moment([ cirq.XPowGate(exponent=0.3, global_shift=1.1)(q0), cirq.YPowGate(exponent=0.4, global_shift=1)(q1), cirq.ZPowGate(exponent=0.5, global_shift=0.9)(q2), cirq.HPowGate(exponent=0.6, global_shift=0.8)(q3), ]), cirq.Moment([ cirq.CX(q0, q2), cirq.CZ(q1, q3), ]), cirq.Moment([ cirq.X(q0), cirq.Y(q1), cirq.Z(q2), cirq.S(q3), ]), cirq.Moment([ cirq.XXPowGate(exponent=0.4, global_shift=0.7)(q0, q1), cirq.YYPowGate(exponent=0.8, global_shift=0.5)(q2, q3), ]), cirq.Moment([cirq.I(q0), cirq.I(q1), cirq.IdentityGate(2)(q2, q3)]), cirq.Moment([ cirq.rx(0.7)(q0), cirq.ry(0.2)(q1), cirq.rz(0.4)(q2), cirq.PhasedXPowGate(phase_exponent=0.8, exponent=0.6, global_shift=0.3)(q3), ]), cirq.Moment([ cirq.ZZPowGate(exponent=0.3, global_shift=1.3)(q0, q2), cirq.ISwapPowGate(exponent=0.6, global_shift=1.2)(q1, q3), ]), cirq.Moment([ cirq.XPowGate(exponent=0.1, global_shift=0.9)(q0), cirq.YPowGate(exponent=0.2, global_shift=1)(q1), cirq.ZPowGate(exponent=0.3, global_shift=1.1)(q2), cirq.HPowGate(exponent=0.4, global_shift=1.2)(q3), ]), cirq.Moment([ cirq.SwapPowGate(exponent=0.2, global_shift=0.9)(q0, q1), cirq.PhasedISwapPowGate(phase_exponent=0.8, exponent=0.6)(q2, q3), ]), cirq.Moment([ cirq.PhasedXZGate(x_exponent=0.2, z_exponent=0.3, axis_phase_exponent=1.4)(q0), cirq.T(q1), cirq.H(q2), cirq.S(q3), ]), cirq.Moment([ cirq.SWAP(q0, q2), cirq.XX(q1, q3), ]), cirq.Moment([ cirq.rx(0.8)(q0), cirq.ry(0.9)(q1), cirq.rz(1.2)(q2), cirq.T(q3), ]), cirq.Moment([ cirq.YY(q0, q1), cirq.ISWAP(q2, q3), ]), cirq.Moment([ cirq.T(q0), cirq.Z(q1), cirq.Y(q2), cirq.X(q3), ]), cirq.Moment([ cirq.FSimGate(0.3, 1.7)(q0, q2), cirq.ZZ(q1, q3), ]), cirq.Moment([ cirq.ry(1.3)(q0), cirq.rz(0.4)(q1), cirq.rx(0.7)(q2), cirq.S(q3), ]), cirq.Moment([ cirq.MatrixGate( np.array([[0, -0.5 - 0.5j, -0.5 - 0.5j, 0], [0.5 - 0.5j, 0, 0, -0.5 + 0.5j], [0.5 - 0.5j, 0, 0, 0.5 - 0.5j], [0, -0.5 - 0.5j, 0.5 + 0.5j, 0]]))(q0, q1), cirq.MatrixGate( np.array([[0.5 - 0.5j, 0, 0, -0.5 + 0.5j], [0, 0.5 - 0.5j, -0.5 + 0.5j, 0], [0, -0.5 + 0.5j, -0.5 + 0.5j, 0], [0.5 - 0.5j, 0, 0, 0.5 - 0.5j]]))(q2, q3), ]), cirq.Moment([ cirq.MatrixGate(np.array([[1, 0], [0, 1j]]))(q0), cirq.MatrixGate(np.array([[0, -1j], [1j, 0]]))(q1), cirq.MatrixGate(np.array([[0, 1], [1, 0]]))(q2), cirq.MatrixGate(np.array([[1, 0], [0, -1]]))(q3), ]), cirq.Moment([ cirq.riswap(0.7)(q0, q1), cirq.givens(1.2)(q2, q3), ]), cirq.Moment([ cirq.H(q0), cirq.H(q1), cirq.H(q2), cirq.H(q3), ]), ) simulator = cirq.Simulator() cirq_result = simulator.simulate(circuit) qsim_simulator = qsimcirq.QSimSimulator() qsim_result = qsim_simulator.simulate(circuit) assert cirq.linalg.allclose_up_to_global_phase( qsim_result.state_vector(), cirq_result.state_vector())
def _get_circuit_proto_pairs(): q0 = cirq.GridQubit(0, 0) q1 = cirq.GridQubit(0, 1) pairs = [ # HPOW and aliases. (cirq.Circuit(cirq.HPowGate(exponent=0.3)(q0)), _build_gate_proto("HP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.HPowGate(exponent=sympy.Symbol('alpha'))(q0)), _build_gate_proto("HP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.HPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0)), _build_gate_proto("HP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0'])), (cirq.Circuit(cirq.H(q0)), _build_gate_proto("HP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0'])), # XPOW and aliases. (cirq.Circuit(cirq.XPowGate(exponent=0.3)(q0)), _build_gate_proto("XP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.XPowGate(exponent=sympy.Symbol('alpha'))(q0)), _build_gate_proto("XP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.XPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0)), _build_gate_proto("XP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0'])), (cirq.Circuit(cirq.X(q0)), _build_gate_proto("XP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0'])), # YPOW and aliases (cirq.Circuit(cirq.YPowGate(exponent=0.3)(q0)), _build_gate_proto("YP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.YPowGate(exponent=sympy.Symbol('alpha'))(q0)), _build_gate_proto("YP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.YPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0)), _build_gate_proto("YP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0'])), (cirq.Circuit(cirq.Y(q0)), _build_gate_proto("YP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0'])), # ZPOW and aliases. (cirq.Circuit(cirq.ZPowGate(exponent=0.3)(q0)), _build_gate_proto("ZP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.ZPowGate(exponent=sympy.Symbol('alpha'))(q0)), _build_gate_proto("ZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.ZPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0)), _build_gate_proto("ZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0'])), (cirq.Circuit(cirq.Z(q0)), _build_gate_proto("ZP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0'])), # XXPow and aliases (cirq.Circuit(cirq.XXPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("XXP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.XXPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("XXP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.XXPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("XXP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.XX(q0, q1)), _build_gate_proto("XXP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # YYPow and aliases (cirq.Circuit(cirq.YYPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("YYP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.YYPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("YYP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.YYPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("YYP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.YY(q0, q1)), _build_gate_proto("YYP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # ZZPow and aliases (cirq.Circuit(cirq.ZZPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("ZZP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.ZZPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("ZZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.ZZPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("ZZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.ZZ(q0, q1)), _build_gate_proto("ZZP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # CZPow and aliases (cirq.Circuit(cirq.CZPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("CZP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.CZPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("CZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.CZPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("CZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.CZ(q0, q1)), _build_gate_proto("CZP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # CNOTPow and aliases (cirq.Circuit(cirq.CNotPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("CNP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.CNotPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("CNP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.CNotPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("CNP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.CNOT(q0, q1)), _build_gate_proto("CNP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # SWAPPow and aliases (cirq.Circuit(cirq.SwapPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("SP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.SwapPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("SP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.SwapPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("SP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.SWAP(q0, q1)), _build_gate_proto("SP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # ISWAPPow and aliases (cirq.Circuit(cirq.ISwapPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("ISP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.ISwapPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("ISP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.ISwapPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("ISP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.ISWAP(q0, q1)), _build_gate_proto("ISP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # PhasedXPow and aliases (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=0.9, exponent=0.3, global_shift=0.2)(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], [0.9, 1.0, 0.3, 1.0, 0.2], ['0_0'])), (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=sympy.Symbol('alpha'), exponent=0.3)(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], ['alpha', 1.0, 0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=3.1 * sympy.Symbol('alpha'), exponent=0.3)(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], ['alpha', 3.1, 0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=0.9, exponent=sympy.Symbol('beta'))(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], [0.9, 1.0, 'beta', 1.0, 0.0], ['0_0'])), (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=0.9, exponent=5.1 * sympy.Symbol('beta'))(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], [0.9, 1.0, 'beta', 5.1, 0.0], ['0_0'])), (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=3.1 * sympy.Symbol('alpha'), exponent=5.1 * sympy.Symbol('beta'))(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], ['alpha', 3.1, 'beta', 5.1, 0.0], ['0_0'])), # RX, RY, RZ with symbolization is tested in special cases as the # string comparison of the float converted sympy.pi does not happen # smoothly. See: test_serialize_deserialize_special_case_one_qubit (cirq.Circuit(cirq.rx(np.pi)(q0)), _build_gate_proto("XP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, -0.5], ['0_0'])), (cirq.Circuit(cirq.ry(np.pi)(q0)), _build_gate_proto("YP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, -0.5], ['0_0'])), (cirq.Circuit(cirq.rz(np.pi)(q0)), _build_gate_proto("ZP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, -0.5], ['0_0'])), # Identity (cirq.Circuit(cirq.I(q0)), _build_gate_proto("I", ['unused'], [True], ['0_0'])), # FSimGate (cirq.Circuit(cirq.FSimGate(theta=0.1, phi=0.2)(q0, q1)), _build_gate_proto("FSIM", ['theta', 'theta_scalar', 'phi', 'phi_scalar'], [0.1, 1.0, 0.2, 1.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.FSimGate(theta=2.1 * sympy.Symbol("alpha"), phi=1.3 * sympy.Symbol("beta"))(q0, q1)), _build_gate_proto("FSIM", ['theta', 'theta_scalar', 'phi', 'phi_scalar'], ['alpha', 2.1, 'beta', 1.3], ['0_0', '0_1'])), ] return pairs
cirq.ry(0.1), cirq.rz(0.1), cirq.H, cirq.HPowGate(exponent=1, global_shift=-0.5), cirq.T, cirq.S, cirq.CNOT, cirq.CXPowGate(exponent=1, global_shift=-0.5), cirq.XX, cirq.YY, cirq.ZZ, cirq.XX**0.5, cirq.YY**0.5, cirq.ZZ**0.5, cirq.SWAP, cirq.SwapPowGate(exponent=1, global_shift=-0.5), cirq.MeasurementGate(num_qubits=1, key='a'), cirq.MeasurementGate(num_qubits=2, key='b'), cirq.MeasurementGate(num_qubits=10, key='c'), ) @pytest.mark.parametrize('gate', VALID_GATES) def test_validate_operation_valid(gate): qubits = cirq.LineQubit.range(gate.num_qubits()) device = ionq.IonQAPIDevice(qubits=qubits) operation = gate(*qubits) device.validate_operation(operation) INVALID_GATES = (
return True @pytest.mark.parametrize( 'gate, expected_length', [ (cast(cirq.Gate, cirq.ISWAP), 7), # cast is for fixing mypy confusion (cirq.CZ, 8), (cirq.SWAP, 7), (cirq.CNOT, 9), (cirq.ISWAP**0.5, 1), (cirq.ISWAP**-0.5, 1), (cirq.ISwapPowGate(exponent=0.5), 1), (cirq.ISwapPowGate(exponent=-0.5), 1), (cirq.FSimGate(theta=np.pi / 4, phi=0), 1), *[(cirq.SwapPowGate(exponent=a), 13) for a in np.linspace(0, 2 * np.pi, 20)], *[(cirq.CZPowGate(exponent=a), 8) for a in np.linspace(0, 2 * np.pi, 20)], *[(cirq.ISwapPowGate(exponent=a), 5) for a in np.linspace(0, 2 * np.pi, 20)], *[(cirq.CNotPowGate(exponent=a), 9) for a in np.linspace(0, 2 * np.pi, 20)], *[(cirq.FSimGate(theta=a, phi=a), 13) for a in np.linspace(0, 2 * np.pi, 20)], ], ) def test_two_qubit_gates(gate: cirq.Gate, expected_length: int): """Tests that two qubit gates decompose to an equivalent and serializable circuit with the expected length (or less). """
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], 'SYC': cirq.google.SYC, 'SycamoreGate': cirq.google.SycamoreGate(), 'T': cirq.T, 'TOFFOLI': cirq.TOFFOLI, 'TwoQubitMatrixGate': cirq.TwoQubitMatrixGate(np.eye(4)), 'UNCONSTRAINED_DEVICE': cirq.UNCONSTRAINED_DEVICE, 'WaitGate': cirq.WaitGate(cirq.Duration(nanos=10)), '_QubitAsQid': [
worst_diff = np.max(np.abs(u_decomp - u_target)) assert False, ( f'Unitaries do not match closely enough (atol={atol}, rtol={rtol}, ' f'worst element diff={worst_diff}).') def assert_specific_sqrt_iswap_count(operations, count): actual = sum(len(op.qubits) == 2 for op in operations) assert actual == count, f'Incorrect sqrt-iSWAP count. Expected {count} but got {actual}.' @pytest.mark.parametrize( 'gate', [ cirq.ISwapPowGate(exponent=sympy.Symbol('t')), cirq.SwapPowGate(exponent=sympy.Symbol('t')), cirq.CZPowGate(exponent=sympy.Symbol('t')), ], ) def test_two_qubit_gates_with_symbols(gate: cirq.Gate): op = gate(*cirq.LineQubit.range(2)) c_new_sqrt_iswap = cirq.Circuit( cirq.parameterized_2q_op_to_sqrt_iswap_operations(op)) c_new_sqrt_iswap_inv = cirq.Circuit( cirq.parameterized_2q_op_to_sqrt_iswap_operations( op, use_sqrt_iswap_inv=True)) # Check if unitaries are the same for val in np.linspace(0, 2 * np.pi, 12): cirq.testing.assert_allclose_up_to_global_phase( cirq.unitary(cirq.resolve_parameters(op, {'t': val})), cirq.unitary(cirq.resolve_parameters(c_new_sqrt_iswap,
# ndtotext_print(qc_simple.unitary()) equal = (qc_simple.unitary() - qc_true.unitary() < 1e-5).all() print(f'Simple circuit equal U: {equal}') assert equal # TODO: print to latex # latex = cirq.contrib.qcircuit.circuit_to_latex_using_qcircuit(qc_simple) # print(latex) # --- Alternative with SWAP instead of CZ --- qc2 = cirq.Circuit() # --- All 3 --- A2B_XX_B2A(qc2, T1, T2) # This is to do the oper |11> <-> |+>, |00> <-> |-> # CCSWAP(t/2) qc2.append(cirq.SwapPowGate().on(T1, T2).controlled_by(C1, C2)**(t/π)) A2B_XX_B2A(qc2, C1, C2) qc2.append(cirq.ISwapPowGate().on(T1, T2).controlled_by(C1, C2)**(-2*t/π)) # NOTICE: Swapped signs and cancel out π/2 qc2.append(cirq.ISwapPowGate().on(C1, C2).controlled_by(T1, T2)**(2*t/π)) A2B_XX_B2A(qc2, T1, T2) # CCSWAP(-t/2) qc2.append(cirq.SwapPowGate().on(C1, C2).controlled_by(T1, T2)**(-t/π)) A2B_XX_B2A(qc2, C1, C2) # This is to do the oper |11> <-> |+>, |00> <-> |-> (i.e. go back) # --- All 3 end --- print(qc2) equal = (qc2.unitary() - qc_true.unitary() < 1e-5).all() print(f'New circuit equal U: {equal}')
return cirq.allclose_up_to_global_phase(unitary1, unitary2) @pytest.mark.parametrize( 'gate, expected_length', [ (cast(cirq.Gate, cirq.ISWAP), 7), # cast is for fixing mypy confusion (cirq.CZ, 8), (cirq.SWAP, 7), (cirq.CNOT, 9), (cirq.ISWAP ** 0.5, 1), (cirq.ISWAP ** -0.5, 1), (cirq.ISwapPowGate(exponent=0.5), 1), (cirq.ISwapPowGate(exponent=-0.5), 1), (cirq.FSimGate(theta=np.pi / 4, phi=0), 1), *[(cirq.SwapPowGate(exponent=a), 13) for a in np.linspace(0, 2 * np.pi, 20)], *[(cirq.CZPowGate(exponent=a), 8) for a in np.linspace(0, 2 * np.pi, 20)], *[(cirq.ISwapPowGate(exponent=a), 5) for a in np.linspace(0, 2 * np.pi, 20)], *[(cirq.CNotPowGate(exponent=a), 9) for a in np.linspace(0, 2 * np.pi, 20)], *[(cirq.FSimGate(theta=a, phi=a), 13) for a in np.linspace(0, 2 * np.pi, 20)], ], ) def test_two_qubit_gates(gate: cirq.Gate, expected_length: int): """Tests that two qubit gates decompose to an equivalent and serializable circuit with the expected length (or less). """ q0 = cirq.GridQubit(5, 3) q1 = cirq.GridQubit(5, 4) original_circuit = cirq.Circuit(gate(q0, q1)) converted_circuit = original_circuit.copy() cgoc.ConvertToSqrtIswapGates().optimize_circuit(converted_circuit)
def test_apply_gate(): q0, q1 = cirq.LineQubit.range(2) state = Mock() args = cirq.StabilizerSimulationState(state=state, qubits=[q0, q1]) assert args._strat_apply_gate(cirq.X, [q0]) is True state.apply_x.assert_called_with(0, 1.0, 0.0) state.reset_mock() assert args._strat_apply_gate(cirq.X**2, [q0]) is True state.apply_x.assert_called_with(0, 2.0, 0.0) state.reset_mock() assert args._strat_apply_gate(cirq.X**sympy.Symbol('t'), [q0]) is NotImplemented state.apply_x.assert_not_called() state.reset_mock() assert args._strat_apply_gate(cirq.XPowGate(exponent=2, global_shift=1.3), [q1]) is True state.apply_x.assert_called_with(1, 2.0, 1.3) state.reset_mock() assert args._strat_apply_gate(cirq.X**1.4, [q0]) == NotImplemented state.apply_x.assert_not_called() state.reset_mock() assert args._strat_apply_gate(cirq.Y, [q0]) is True state.apply_y.assert_called_with(0, 1.0, 0.0) state.reset_mock() assert args._strat_apply_gate(cirq.Z, [q0]) is True state.apply_z.assert_called_with(0, 1.0, 0.0) state.reset_mock() assert args._strat_apply_gate(cirq.H, [q0]) is True state.apply_h.assert_called_with(0, 1.0, 0.0) state.reset_mock() assert args._strat_apply_gate(cirq.CX, [q0, q1]) is True state.apply_cx.assert_called_with(0, 1, 1.0, 0.0) state.reset_mock() assert args._strat_apply_gate(cirq.CX, [q1, q0]) is True state.apply_cx.assert_called_with(1, 0, 1.0, 0.0) state.reset_mock() assert args._strat_apply_gate(cirq.CZ, [q0, q1]) is True state.apply_cz.assert_called_with(0, 1, 1.0, 0.0) state.reset_mock() assert args._strat_apply_gate(cirq.GlobalPhaseGate(1j), []) is True state.apply_global_phase.assert_called_with(1j) state.reset_mock() assert args._strat_apply_gate(cirq.GlobalPhaseGate(sympy.Symbol('t')), []) is NotImplemented state.apply_global_phase.assert_not_called() state.reset_mock() assert args._strat_apply_gate(cirq.SWAP, [q0, q1]) is True state.apply_cx.assert_has_calls( [call(0, 1), call(1, 0, 1.0, 0.0), call(0, 1)]) state.reset_mock() assert args._strat_apply_gate( cirq.SwapPowGate(exponent=2, global_shift=1.3), [q0, q1]) is True state.apply_cx.assert_has_calls( [call(0, 1), call(1, 0, 2.0, 1.3), call(0, 1)]) state.reset_mock() assert args._strat_apply_gate(cirq.BitFlipChannel(0.5), [q0]) == NotImplemented state.apply_x.assert_not_called()