def _measurement_circuit(): qc = QubitCircuit(2, num_cbits=2) qc.add_measurement("M0", targets=[0], classical_store=0) qc.add_measurement("M1", targets=[1], classical_store=1) return qc
def test_reverse(self): """ Reverse a quantum circuit """ qc = QubitCircuit(3) qc.add_gate("RX", targets=[0], arg_value=3.141, arg_label=r"\pi/2") qc.add_gate("CNOT", targets=[1], controls=[0]) qc.add_measurement("M1", targets=[1]) qc.add_gate("SNOT", targets=[2]) # Keep input output same qc.add_state("0", targets=[0]) qc.add_state("+", targets=[1], state_type="output") qc.add_state("-", targets=[1]) qc_rev = qc.reverse_circuit() assert qc_rev.gates[0].name == "SNOT" assert qc_rev.gates[1].name == "M1" assert qc_rev.gates[2].name == "CNOT" assert qc_rev.gates[3].name == "RX" assert qc_rev.input_states[0] == "0" assert qc_rev.input_states[2] is None assert qc_rev.output_states[1] == "+"
def test_qasm_str(): expected_qasm_str = ('// QASM 2.0 file generated by QuTiP\n\nOPENQASM 2.0;' '\ninclude "qelib1.inc";\n\nqreg q[2];\ncreg c[1];\n\n' 'x q[0];\nmeasure q[1] -> c[0]\n') simple_qc = QubitCircuit(2, num_cbits=1) simple_qc.add_gate("X", targets=[0]) simple_qc.add_measurement("M", targets=[1], classical_store=0) assert circuit_to_qasm_str(simple_qc) == expected_qasm_str
def test_add_circuit(self): """ Addition of a circuit 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_measurement("M0", targets=[0], classical_store=[1]) qc.add_1q_gate("RY", start=4, end=5, arg_value=1.570796) qc1 = QubitCircuit(6) qc1.add_circuit(qc) # Test if all gates and measurements are added assert len(qc1.gates) == len(qc.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)
def _teleportation_circuit(): teleportation = QubitCircuit(3, num_cbits=2, input_states=["q0", "0", "0", "c0", "c1"]) teleportation.add_gate("SNOT", targets=[1]) teleportation.add_gate("CNOT", targets=[2], controls=[1]) teleportation.add_gate("CNOT", targets=[1], controls=[0]) teleportation.add_gate("SNOT", targets=[0]) teleportation.add_measurement("M0", targets=[0], classical_store=1) teleportation.add_measurement("M1", targets=[1], classical_store=0) teleportation.add_gate("X", targets=[2], classical_controls=[0]) teleportation.add_gate("Z", targets=[2], classical_controls=[1]) return teleportation
def test_add_measurement(self): """ Addition of Measurement Object to a circuit. """ qc = QubitCircuit(3, num_cbits=2) qc.add_measurement("M0", targets=[0], classical_store=1) qc.add_gate("CNOT", targets=[1], controls=[0]) qc.add_gate("TOFFOLI", controls=[0, 1], targets=[2]) qc.add_measurement("M1", targets=[2], classical_store=0) qc.add_gate("SNOT", targets=[1], classical_controls=[0, 1]) qc.add_measurement("M2", targets=[1]) # checking correct addition of measurements assert qc.gates[0].targets[0] == 0 assert qc.gates[0].classical_store == 1 assert qc.gates[3].name == "M1" assert qc.gates[5].classical_store is None # checking if gates are added correctly with measurements assert qc.gates[2].name == "TOFFOLI" assert qc.gates[4].classical_controls == [0, 1]
def test_latex_code_non_reversed(self): qc = QubitCircuit(1, num_cbits=1, reverse_states=False) qc.add_measurement("M0", targets=0, classical_store=0) exp = ' & & \\meter & \\qw \\\\ \n & ' + \ '& \\qw \\cwx[-1] & \\qw \\\\ \n' assert qc.latex_code() == self._latex_template % exp
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)