def _ZXZ_rotation(input_gate): r"""An input 1-qubit gate is expressed as a product of rotation matrices :math:`\textrm{R}_z` and :math:`\textrm{R}_x`. Parameters ---------- input_gate : :class:`qutip.Qobj` The matrix that's supposed to be decomposed should be a Qobj. """ check_gate(input_gate, num_qubits=1) alpha, theta, beta, global_phase_angle = _angles_for_ZYZ(input_gate) alpha = alpha - np.pi / 2 beta = beta + np.pi / 2 # theta and global phase are same as ZYZ values Phase_gate = Gate( "GLOBALPHASE", targets=[0], arg_value=global_phase_angle, arg_label=r"{:0.2f} \times \pi".format(global_phase_angle / np.pi), ) Rz_alpha = Gate( "RZ", targets=[0], arg_value=alpha, arg_label=r"{:0.2f} \times \pi".format(alpha / np.pi), ) Rx_theta = Gate( "RX", targets=[0], arg_value=theta, arg_label=r"{:0.2f} \times \pi".format(theta / np.pi), ) Rz_beta = Gate( "RZ", targets=[0], arg_value=beta, arg_label=r"{:0.2f} \times \pi".format(beta / np.pi), ) return (Rz_alpha, Rx_theta, Rz_beta, Phase_gate)
def test_scheduling_pulse(instructions, method, expected_length, random_shuffle, gates_schedule): circuit = QubitCircuit(4) for instruction in instructions: circuit.add_gate( Gate(instruction.name, instruction.targets, instruction.controls)) if random_shuffle: repeat_num = 5 else: repeat_num = 0 result0 = gate_sequence_product(circuit.propagators()) # run the scheduler scheduler = Scheduler(method) gate_cycle_indices = scheduler.schedule(instructions, gates_schedule=gates_schedule, repeat_num=repeat_num) assert max(gate_cycle_indices) == expected_length
def _ZYZ_pauli_X(input_gate): """Returns a 1 qubit unitary as a product of ZYZ rotation matrices and Pauli X.""" check_gate(input_gate, num_qubits=1) alpha, theta, beta, global_phase_angle = _angles_for_ZYZ(input_gate) Phase_gate = Gate( "GLOBALPHASE", targets=[0], arg_value=global_phase_angle, arg_label=r"{:0.2f} \times \pi".format(global_phase_angle / np.pi), ) Rz_A = Gate( "RZ", targets=[0], arg_value=alpha, arg_label=r"{:0.2f} \times \pi".format(alpha / np.pi), ) Ry_A = Gate( "RY", targets=[0], arg_value=theta / 2, arg_label=r"{:0.2f} \times \pi".format(theta / np.pi), ) Pauli_X = Gate("X", targets=[0]) Ry_B = Gate( "RY", targets=[0], arg_value=-theta / 2, arg_label=r"{:0.2f} \times \pi".format(-theta / np.pi), ) Rz_B = Gate( "RZ", targets=[0], arg_value=-(alpha + beta) / 2, arg_label=r"{:0.2f} \times \pi".format(-(alpha + beta) / (2 * np.pi)), ) Rz_C = Gate( "RZ", targets=[0], arg_value=(-alpha + beta) / 2, arg_label=r"{:0.2f} \times \pi".format((-alpha + beta) / (2 * np.pi)), ) return (Rz_A, Ry_A, Pauli_X, Ry_B, Rz_B, Pauli_X, Rz_C, Phase_gate)
import qutip from qutip_qip.circuit import QubitCircuit from qutip_qip.operations import Gate, gate_sequence_product from qutip_qip.device import (DispersiveCavityQED, LinearSpinChain, CircularSpinChain, SCQubits) from packaging.version import parse as parse_version if parse_version(qutip.__version__) < parse_version('5.dev'): from qutip import Options as SolverOptions else: from qutip import SolverOptions _tol = 3.e-2 _x = Gate("X", targets=[0]) _z = Gate("Z", targets=[0]) _y = Gate("Y", targets=[0]) _snot = Gate("SNOT", targets=[0]) _rz = Gate("RZ", targets=[0], arg_value=np.pi / 2, arg_label=r"\pi/2") _rx = Gate("RX", targets=[0], arg_value=np.pi / 2, arg_label=r"\pi/2") _ry = Gate("RY", targets=[0], arg_value=np.pi / 2, arg_label=r"\pi/2") _iswap = Gate("ISWAP", targets=[0, 1]) _cnot = Gate("CNOT", targets=[0], controls=[1]) _sqrt_iswap = Gate("SQRTISWAP", targets=[0, 1]) single_gate_tests = [ pytest.param(2, [_z], id="Z"), pytest.param(2, [_x], id="X"), pytest.param(2, [_y], id="Y"), pytest.param(2, [_snot], id="SNOT"),
def test_add_circuit(self): """ Addition of a circuit to a `QubitCircuit` """ def customer_gate1(arg_values): mat = np.zeros((4, 4), dtype=np.complex128) mat[0, 0] = mat[1, 1] = 1. mat[2:4, 2:4] = gates.rx(arg_values) return Qobj(mat, dims=[[2, 2], [2, 2]]) qc = QubitCircuit(6) qc.user_gates = {"CTRLRX": customer_gate1} qc = QubitCircuit(6) qc.add_gate("CNOT", targets=[1], controls=[0]) test_gate = Gate("SWAP", targets=[1, 4]) qc.add_gate(test_gate) qc.add_gate("TOFFOLI", controls=[0, 1], targets=[2]) qc.add_gate("SNOT", targets=[3]) qc.add_gate(test_gate, index=[3]) qc.add_measurement("M0", targets=[0], classical_store=[1]) qc.add_1q_gate("RY", start=4, end=5, arg_value=1.570796) qc.add_gate("CTRLRX", targets=[1, 2], arg_value=np.pi/2) qc1 = QubitCircuit(6) qc1.add_circuit(qc) # Test if all gates and measurements are added assert len(qc1.gates) == len(qc.gates) # Test if the definitions of user gates are added assert qc1.user_gates == qc.user_gates for i in range(len(qc1.gates)): assert (qc1.gates[i].name == qc.gates[i].name) assert (qc1.gates[i].targets == qc.gates[i].targets) if (isinstance(qc1.gates[i], Gate) and isinstance(qc.gates[i], Gate)): assert (qc1.gates[i].controls == qc.gates[i].controls) assert (qc1.gates[i].classical_controls == qc.gates[i].classical_controls) elif (isinstance(qc1.gates[i], Measurement) and isinstance(qc.gates[i], Measurement)): assert (qc1.gates[i].classical_store == qc.gates[i].classical_store) # Test exception when qubit out of range pytest.raises(NotImplementedError, qc1.add_circuit, qc, start=4) qc2 = QubitCircuit(8) qc2.add_circuit(qc, start=2) # Test if all gates are added assert len(qc2.gates) == len(qc.gates) # Test if the positions are correct for i in range(len(qc2.gates)): if qc.gates[i].targets is not None: assert (qc2.gates[i].targets[0] == qc.gates[i].targets[0]+2) if (isinstance(qc.gates[i], Gate) and qc.gates[i].controls is not None): assert (qc2.gates[i].controls[0] == qc.gates[i].controls[0]+2) # Test exception when the operators to be added are not gates or measurements qc.gates[-1] = 0 pytest.raises(TypeError, qc2.add_circuit, qc)
def test_add_gate(self): """ Addition of a gate object directly to a `QubitCircuit` """ qc = QubitCircuit(6) qc.add_gate("CNOT", targets=[1], controls=[0]) test_gate = Gate("SWAP", targets=[1, 4]) qc.add_gate(test_gate) qc.add_gate("TOFFOLI", controls=[0, 1], targets=[2]) qc.add_gate("SNOT", targets=[3]) qc.add_gate(test_gate, index=[3]) qc.add_1q_gate("RY", start=4, end=5, arg_value=1.570796) # Test explicit gate addition assert qc.gates[0].name == "CNOT" assert qc.gates[0].targets == [1] assert qc.gates[0].controls == [0] # Test direct gate addition assert qc.gates[1].name == test_gate.name assert qc.gates[1].targets == test_gate.targets # Test specified position gate addition assert qc.gates[3].name == test_gate.name assert qc.gates[3].targets == test_gate.targets # Test adding 1 qubit gate on [start, end] qubits assert qc.gates[5].name == "RY" assert qc.gates[5].targets == [4] assert qc.gates[5].arg_value == 1.570796 assert qc.gates[6].name == "RY" assert qc.gates[6].targets == [5] assert qc.gates[5].arg_value == 1.570796 dummy_gate1 = Gate("DUMMY1") inds = [1, 3, 4, 6] qc.add_gate(dummy_gate1, index=inds) # Test adding gates at multiple (sorted) indices at once. # NOTE: Every insertion shifts the indices in the original list of # gates by an additional position to the right. expected_gate_names = [ 'CNOT', # 0 'DUMMY1', # 1 'SWAP', # 2 'TOFFOLI', # 3 'DUMMY1', # 4 'SWAP', # 5 'DUMMY1', # 6 'SNOT', # 7 'RY', # 8 'DUMMY1', # 9 'RY', # 10 ] actual_gate_names = [gate.name for gate in qc.gates] assert actual_gate_names == expected_gate_names dummy_gate2 = Gate("DUMMY2") inds = [11, 0] qc.add_gate(dummy_gate2, index=inds) # Test adding gates at multiple (unsorted) indices at once. expected_gate_names = [ 'DUMMY2', # 0 'CNOT', # 1 'DUMMY1', # 2 'SWAP', # 3 'TOFFOLI', # 4 'DUMMY1', # 5 'SWAP', # 6 'DUMMY1', # 7 'SNOT', # 8 'RY', # 9 'DUMMY1', # 10 'RY', # 11 'DUMMY2', # 12 ] actual_gate_names = [gate.name for gate in qc.gates] assert actual_gate_names == expected_gate_names
def test_deprecation_warning(self): # Make them available for backward compatibility. with pytest.warns(DeprecationWarning): from qutip_qip.circuit import Gate, Measurement Gate("X", 0)