def _define(self): """ gate rccx a,b,c { u2(0,pi) c; u1(pi/4) c; cx b, c; u1(-pi/4) c; cx a, c; u1(pi/4) c; cx b, c; u1(-pi/4) c; u2(0,pi) c; } """ definition = [] q = QuantumRegister(3, 'q') rule = [ (U2Gate(0, pi), [q[2]], []), # H gate (U1Gate(pi / 4), [q[2]], []), # T gate (CnotGate(), [q[1], q[2]], []), (U1Gate(-pi / 4), [q[2]], []), # inverse T gate (CnotGate(), [q[0], q[2]], []), (U1Gate(pi / 4), [q[2]], []), (CnotGate(), [q[1], q[2]], []), (U1Gate(-pi / 4), [q[2]], []), # inverse T gate (U2Gate(0, pi), [q[2]], []), # H gate ] for inst in rule: definition.append(inst) self.definition = definition
def test_dag_collect_runs(self): """Test the collect_runs method with 3 different gates.""" self.dag.apply_operation_back(U1Gate(3.14), [self.qubit0]) self.dag.apply_operation_back(U1Gate(3.14), [self.qubit0]) self.dag.apply_operation_back(U1Gate(3.14), [self.qubit0]) self.dag.apply_operation_back(CnotGate(), [self.qubit2, self.qubit1]) self.dag.apply_operation_back(CnotGate(), [self.qubit1, self.qubit2]) self.dag.apply_operation_back(HGate(), [self.qubit2]) collected_runs = self.dag.collect_runs(['u1', 'cx', 'h']) self.assertEqual(len(collected_runs), 3) for run in collected_runs: if run[0].name == 'cx': self.assertEqual(len(run), 2) self.assertEqual(['cx'] * 2, [x.name for x in run]) self.assertEqual( [[self.qubit2, self.qubit1], [self.qubit1, self.qubit2]], [x.qargs for x in run]) elif run[0].name == 'h': self.assertEqual(len(run), 1) self.assertEqual(['h'], [x.name for x in run]) self.assertEqual([[self.qubit2]], [x.qargs for x in run]) elif run[0].name == 'u1': self.assertEqual(len(run), 3) self.assertEqual(['u1'] * 3, [x.name for x in run]) self.assertEqual([[self.qubit0], [self.qubit0], [self.qubit0]], [x.qargs for x in run]) else: self.fail('Unknown run encountered')
def _define(self): """ gate cu3(theta,phi,lambda) c, t { u1((lambda+phi)/2) c; u1((lambda-phi)/2) t; cx c,t; u3(-theta/2,0,-(phi+lambda)/2) t; cx c,t; u3(theta/2,phi,0) t; } """ from qiskit.extensions.standard.u1 import U1Gate from qiskit.extensions.standard.x import CnotGate definition = [] q = QuantumRegister(2, "q") rule = [ (U1Gate((self.params[2] + self.params[1]) / 2), [q[0]], []), (U1Gate((self.params[2] - self.params[1]) / 2), [q[1]], []), (CnotGate(), [q[0], q[1]], []), (U3Gate(-self.params[0] / 2, 0, -(self.params[1] + self.params[2]) / 2), [q[1]], []), (CnotGate(), [q[0], q[1]], []), (U3Gate(self.params[0] / 2, self.params[1], 0), [q[1]], []) ] for inst in rule: definition.append(inst) self.definition = definition
def _define(self): """ gate cu3(theta,phi,lambda) c, t { u1(pi/2) t; cx c,t; u3(-theta/2,0,0) t; cx c,t; u3(theta/2,-pi/2,0) t; } """ from qiskit.extensions.standard.u1 import U1Gate from qiskit.extensions.standard.u3 import U3Gate from qiskit.extensions.standard.x import CnotGate definition = [] q = QuantumRegister(2, 'q') rule = [ (U1Gate(pi / 2), [q[1]], []), (CnotGate(), [q[0], q[1]], []), (U3Gate(-self.params[0] / 2, 0, 0), [q[1]], []), (CnotGate(), [q[0], q[1]], []), (U3Gate(self.params[0] / 2, -pi / 2, 0), [q[1]], []) ] for inst in rule: definition.append(inst) self.definition = definition
def _define(self): definition = [] q = QuantumRegister(2) rule = [(CnotGate(), [q[1], q[0]], []), (CryGate(-self.angle), [q[0], q[1]], []), (CnotGate(), [q[1], q[0]], [])] for inst in rule: definition.append(inst) self.definition = definition
def _define(self): self.definition = [] q = QuantumRegister(self.num_qubits) for i in range(self.num_qubits // 2): self.definition.append( (CnotGate(), [q[i], q[self.num_qubits - i - 1]], [])) self.definition.append( (CnotGate(), [q[self.num_qubits - i - 1], q[i]], [])) self.definition.append( (CnotGate(), [q[i], q[self.num_qubits - i - 1]], []))
def _define(self): """ gate swap a,b { cx a,b; cx b,a; cx a,b; } """ from qiskit.extensions.standard.x import CnotGate definition = [] q = QuantumRegister(2, "q") rule = [(CnotGate(), [q[0], q[1]], []), (CnotGate(), [q[1], q[0]], []), (CnotGate(), [q[0], q[1]], [])] for inst in rule: definition.append(inst) self.definition = definition
def test_remove_op_node_longer(self): """Test remove_op_node method in a "longer" dag""" self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1]) self.dag.apply_operation_back(HGate(), [self.qubit0]) self.dag.apply_operation_back(CnotGate(), [self.qubit2, self.qubit1]) self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit2]) self.dag.apply_operation_back(HGate(), [self.qubit2]) op_nodes = list(self.dag.topological_op_nodes()) self.dag.remove_op_node(op_nodes[0]) expected = [('h', [self.qubit0]), ('cx', [self.qubit2, self.qubit1]), ('cx', [self.qubit0, self.qubit2]), ('h', [self.qubit2])] self.assertEqual(expected, [(i.name, i.qargs) for i in self.dag.topological_op_nodes()])
def test_instructions_equal(self): """Test equality of two instructions.""" hop1 = Instruction('h', 1, 0, []) hop2 = Instruction('s', 1, 0, []) hop3 = Instruction('h', 1, 0, []) self.assertFalse(hop1 == hop2) self.assertTrue(hop1 == hop3) uop1 = Instruction('u', 1, 0, [0.4, 0.5, 0.5]) uop2 = Instruction('u', 1, 0, [0.4, 0.6, 0.5]) uop3 = Instruction('v', 1, 0, [0.4, 0.5, 0.5]) uop4 = Instruction('u', 1, 0, [0.4, 0.5, 0.5]) self.assertFalse(uop1 == uop2) self.assertTrue(uop1 == uop4) self.assertFalse(uop1 == uop3) self.assertTrue(HGate() == HGate()) self.assertFalse(HGate() == CnotGate()) self.assertFalse(hop1 == HGate()) eop1 = Instruction('kraus', 1, 0, [np.array([[1, 0], [0, 1]])]) eop2 = Instruction('kraus', 1, 0, [np.array([[0, 1], [1, 0]])]) eop3 = Instruction('kraus', 1, 0, [np.array([[1, 0], [0, 1]])]) eop4 = Instruction('kraus', 1, 0, [np.eye(4)]) self.assertTrue(eop1 == eop3) self.assertFalse(eop1 == eop2) self.assertFalse(eop1 == eop4)
def test_layers_basic(self): """The layers() method returns a list of layers, each of them with a list of nodes.""" qreg = QuantumRegister(2, 'qr') creg = ClassicalRegister(2, 'cr') qubit0 = qreg[0] qubit1 = qreg[1] clbit0 = creg[0] clbit1 = creg[1] condition = (creg, 3) dag = DAGCircuit() dag.add_qreg(qreg) dag.add_creg(creg) dag.apply_operation_back(HGate(), [qubit0], []) dag.apply_operation_back(CnotGate(), [qubit0, qubit1], [], condition=None) dag.apply_operation_back(Measure(), [qubit1, clbit1], [], condition=None) dag.apply_operation_back(XGate(), [qubit1], [], condition=condition) dag.apply_operation_back(Measure(), [qubit0, clbit0], [], condition=None) dag.apply_operation_back(Measure(), [qubit1, clbit1], [], condition=None) layers = list(dag.layers()) self.assertEqual(5, len(layers)) name_layers = [[ node.op.name for node in layer["graph"].nodes() if node.type == "op" ] for layer in layers] self.assertEqual( [['h'], ['cx'], ['measure'], ['x'], ['measure', 'measure']], name_layers)
def _define(self): """ gate ch a,b { s b; h b; t b; cx a, b; tdg b; h b; sdg b; } """ from qiskit.extensions.standard.s import SGate, SdgGate from qiskit.extensions.standard.t import TGate, TdgGate from qiskit.extensions.standard.x import CnotGate definition = [] q = QuantumRegister(2, "q") rule = [ (SGate(), [q[1]], []), (HGate(), [q[1]], []), (TGate(), [q[1]], []), (CnotGate(), [q[0], q[1]], []), (TdgGate(), [q[1]], []), (HGate(), [q[1]], []), (SdgGate(), [q[1]], []) ] for inst in rule: definition.append(inst) self.definition = definition
def test_substituting_node_preserves_args_condition(self, inplace): """Verify args and condition are preserved by a substitution.""" dag = DAGCircuit() qr = QuantumRegister(2) cr = ClassicalRegister(1) dag.add_qreg(qr) dag.add_creg(cr) dag.apply_operation_back(HGate(), [qr[1]]) node_to_be_replaced = dag.apply_operation_back(CnotGate(), [qr[1], qr[0]], condition=(cr, 1)) dag.apply_operation_back(HGate(), [qr[1]]) replacement_node = dag.substitute_node(node_to_be_replaced, CzGate(), inplace=inplace) raise_if_dagcircuit_invalid(dag) self.assertEqual(replacement_node.name, 'cz') self.assertEqual(replacement_node.qargs, [qr[1], qr[0]]) self.assertEqual(replacement_node.cargs, []) self.assertEqual(replacement_node.condition, (cr, 1)) self.assertEqual(replacement_node is node_to_be_replaced, inplace)
def evolve_in_real_time(logfile, qc, t, c): # definizione di U3 --- https://qiskit-staging.mybluemix.net/documentation/terra/summary_of_quantum_operations.html H, U = get_matrices(t, c) two_qubit_cnot_decompose = TwoQubitBasisDecomposer(CnotGate()) C = two_qubit_cnot_decompose.__call__(U) i, j = 0, 1 parameter_string = [] for g in C: instruction, q1, q2 = g if (instruction.name == 'u3'): t1, t2, t3 = instruction.params for x in [t1, t2, t3]: parameter_string.append(round(x, 4)) if (q1[0].index == 0): idx = i else: idx = j qc.u3(t1, t2, t3, idx) if (instruction.name == 'cx'): if (q1[0].index == 0): idx_ctrl = i idx_targ = j else: idx_ctrl = j idx_targ = i qc.cx(idx_ctrl, idx_targ) logfile.write("time = %f \n" % (t)) #logfile.write("circuit: \n"+str(qc.draw())+"\n") ##logfile.write(qc.draw()) return qc
def _define(self): """ gate cswap a,b,c { cx c,b; ccx a,b,c; cx c,b; } """ from qiskit.extensions.standard.x import CnotGate, ToffoliGate definition = [] q = QuantumRegister(3, "q") rule = [(CnotGate(), [q[2], q[1]], []), (ToffoliGate(), [q[0], q[1], q[2]], []), (CnotGate(), [q[2], q[1]], [])] for inst in rule: definition.append(inst) self.definition = definition
def test_topological_op_nodes(self): """The topological_op_nodes() method""" self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(HGate(), [self.qubit0], []) self.dag.apply_operation_back(CnotGate(), [self.qubit2, self.qubit1], []) self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit2], []) self.dag.apply_operation_back(HGate(), [self.qubit2], []) named_nodes = self.dag.topological_op_nodes() expected = [('cx', [self.qubit0, self.qubit1]), ('h', [self.qubit0]), ('cx', [self.qubit2, self.qubit1]), ('cx', [self.qubit0, self.qubit2]), ('h', [self.qubit2])] self.assertEqual(expected, [(i.name, i.qargs) for i in named_nodes])
def _process_cnot(self, node): """Process a CNOT gate node.""" id0 = self._process_bit_id(node.children[0]) id1 = self._process_bit_id(node.children[1]) if not (len(id0) == len(id1) or len(id0) == 1 or len(id1) == 1): raise QiskitError("internal error: qreg size mismatch", "line=%s" % node.line, "file=%s" % node.file) maxidx = max([len(id0), len(id1)]) for idx in range(maxidx): if len(id0) > 1 and len(id1) > 1: self.dag.apply_operation_back(CnotGate(), [id0[idx], id1[idx]], [], self.condition) elif len(id0) > 1: self.dag.apply_operation_back(CnotGate(), [id0[idx], id1[0]], [], self.condition) else: self.dag.apply_operation_back(CnotGate(), [id0[0], id1[idx]], [], self.condition)
def _define(self): """ gate crz(lambda) a,b { u1(lambda/2) b; cx a,b; u1(-lambda/2) b; cx a,b; } """ from qiskit.extensions.standard.x import CnotGate from qiskit.extensions.standard.u1 import U1Gate definition = [] q = QuantumRegister(2, "q") rule = [(U1Gate(self.params[0] / 2), [q[1]], []), (CnotGate(), [q[0], q[1]], []), (U1Gate(-self.params[0] / 2), [q[1]], []), (CnotGate(), [q[0], q[1]], [])] for inst in rule: definition.append(inst) self.definition = definition
def test_substituting_node_with_wrong_width_node_raises(self): """Verify replacing a node with one of a different shape raises.""" dag = DAGCircuit() qr = QuantumRegister(2) dag.add_qreg(qr) node_to_be_replaced = dag.apply_operation_back(CnotGate(), [qr[0], qr[1]]) with self.assertRaises(DAGCircuitError) as _: dag.substitute_node(node_to_be_replaced, Measure())
def test_get_named_nodes(self): """The get_named_nodes(AName) method returns all the nodes with name AName""" self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(HGate(), [self.qubit0], []) self.dag.apply_operation_back(CnotGate(), [self.qubit2, self.qubit1], []) self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit2], []) self.dag.apply_operation_back(HGate(), [self.qubit2], []) # The ordering is not assured, so we only compare the output (unordered) sets. # We use tuples because lists aren't hashable. named_nodes = self.dag.named_nodes('cx') node_qargs = {tuple(node.qargs) for node in named_nodes} expected_qargs = {(self.qubit0, self.qubit1), (self.qubit2, self.qubit1), (self.qubit0, self.qubit2)} self.assertEqual(expected_qargs, node_qargs)
def test_get_op_nodes_all(self): """The method dag.op_nodes() returns all op nodes""" self.dag.apply_operation_back(HGate(), [self.qubit0], []) self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(Reset(), [self.qubit0], []) op_nodes = self.dag.op_nodes() self.assertEqual(len(op_nodes), 3) for node in op_nodes: self.assertIsInstance(node.op, Instruction)
def _define(self): """Calculate a subcircuit that implements this unitary.""" from qiskit.extensions.standard.x import CnotGate from qiskit.extensions.standard.u1 import U1Gate from qiskit.extensions.standard.u2 import U2Gate from qiskit.extensions.standard.u3 import U3Gate from qiskit.extensions.standard.h import HGate definition = [] q = QuantumRegister(2, "q") theta = self.params[0] rule = [ (U3Gate(np.pi / 2, theta, 0), [q[0]], []), (HGate(), [q[1]], []), (CnotGate(), [q[0], q[1]], []), (U1Gate(-theta), [q[1]], []), (CnotGate(), [q[0], q[1]], []), (HGate(), [q[1]], []), (U2Gate(-np.pi, np.pi - theta), [q[0]], []), ] for inst in rule: definition.append(inst) self.definition = definition
def test_dag_nodes_on_wire_multiple_successors(self): """ Test that if a DAGNode has multiple successors in the DAG along one wire, they are all retrieved in order. This could be the case for a circuit such as q0_0: |0>──■─────────■── ┌─┴─┐┌───┐┌─┴─┐ q0_1: |0>┤ X ├┤ H ├┤ X ├ └───┘└───┘└───┘ Both the 2nd CX gate and the H gate follow the first CX gate in the DAG, so they both must be returned but in the correct order. """ self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(HGate(), [self.qubit1], []) self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1], []) nodes = self.dag.nodes_on_wire(self.dag.qubits()[1], only_ops=True) node_names = [nd.name for nd in nodes] self.assertEqual(node_names, ['cx', 'h', 'cx'])
def test_get_gates_nodes(self): """The method dag.gate_nodes() returns all gate nodes""" self.dag.apply_operation_back(HGate(), [self.qubit0], []) self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(Reset(), [self.qubit0], []) op_nodes = self.dag.gate_nodes() self.assertEqual(len(op_nodes), 2) op_node_1 = op_nodes.pop() op_node_2 = op_nodes.pop() self.assertIsInstance(op_node_1.op, Gate) self.assertIsInstance(op_node_2.op, Gate)
def test_two_q_gates(self): """The method dag.twoQ_gates() returns all 2Q gate nodes""" self.dag.apply_operation_back(HGate(), [self.qubit0], []) self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(Barrier(2), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(Reset(), [self.qubit0], []) op_nodes = self.dag.twoQ_gates() self.assertEqual(len(op_nodes), 1) op_node = op_nodes.pop() self.assertIsInstance(op_node.op, Gate) self.assertEqual(len(op_node.qargs), 2)
def test_substitute_circuit_one_middle(self): """The method substitute_node_with_dag() replaces a in-the-middle node with a DAG.""" cx_node = self.dag.op_nodes(op=CnotGate).pop() flipped_cx_circuit = DAGCircuit() v = QuantumRegister(2, "v") flipped_cx_circuit.add_qreg(v) flipped_cx_circuit.apply_operation_back(HGate(), [v[0]], []) flipped_cx_circuit.apply_operation_back(HGate(), [v[1]], []) flipped_cx_circuit.apply_operation_back(CnotGate(), [v[1], v[0]], []) flipped_cx_circuit.apply_operation_back(HGate(), [v[0]], []) flipped_cx_circuit.apply_operation_back(HGate(), [v[1]], []) self.dag.substitute_node_with_dag(cx_node, flipped_cx_circuit, wires=[v[0], v[1]]) self.assertEqual(self.dag.count_ops()['h'], 5)
def setUp(self): self.dag = DAGCircuit() qreg = QuantumRegister(3, 'qr') creg = ClassicalRegister(2, 'cr') self.dag.add_qreg(qreg) self.dag.add_creg(creg) self.qubit0 = qreg[0] self.qubit1 = qreg[1] self.qubit2 = qreg[2] self.clbit0 = creg[0] self.clbit1 = creg[1] self.condition = (creg, 3) self.dag.apply_operation_back(HGate(), [self.qubit0], []) self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(XGate(), [self.qubit1], [])
def evolve_dimer(self,qc,i,j,dt): # c=self.B H,U = get_matrices(dt,c) two_qubit_cnot_decompose = TwoQubitBasisDecomposer(CnotGate()) C = two_qubit_cnot_decompose.__call__(U) parameter_string = [] for g in C: instruction,q1,q2 = g if(instruction.name=='u3'): t1,t2,t3 = instruction.params for x in [t1,t2,t3]: parameter_string.append(round(x,4)) if(q1[0].index==0): idx = i else: idx = j qc.u3(t1,t2,t3,idx) if(instruction.name=='cx'): if(q1[0].index==0): idx_ctrl = i; idx_targ = j else: idx_ctrl = j; idx_targ = i qc.cx(idx_ctrl,idx_targ) return qc
def test_apply_operation_back(self): """The apply_operation_back() method.""" self.dag.apply_operation_back(HGate(), [self.qubit0], [], condition=None) self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1], [], condition=None) self.dag.apply_operation_back(Measure(), [self.qubit1, self.clbit1], [], condition=None) self.dag.apply_operation_back(XGate(), [self.qubit1], [], condition=self.condition) self.dag.apply_operation_back(Measure(), [self.qubit0, self.clbit0], [], condition=None) self.dag.apply_operation_back(Measure(), [self.qubit1, self.clbit1], [], condition=None) self.assertEqual(len(list(self.dag.nodes())), 16) self.assertEqual(len(list(self.dag.edges())), 17)
def test_quantum_predecessors(self): """The method dag.quantum_predecessors() returns predecessors connected by quantum edges""" self.dag.apply_operation_back(Reset(), [self.qubit0], []) self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(Measure(), [self.qubit1, self.clbit1], []) predecessor_measure = self.dag.quantum_predecessors( self.dag.named_nodes('measure').pop()) cnot_node = next(predecessor_measure) with self.assertRaises(StopIteration): next(predecessor_measure) self.assertIsInstance(cnot_node.op, CnotGate) predecessor_cnot = self.dag.quantum_predecessors(cnot_node) self.assertIsInstance(next(predecessor_cnot).op, Reset) self.assertEqual(next(predecessor_cnot).type, 'in') with self.assertRaises(StopIteration): next(predecessor_cnot)
def test_dag_nodes_on_wire(self): """Test that listing the gates on a qubit/classical bit gets the correct gates""" self.dag.apply_operation_back(CnotGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(HGate(), [self.qubit0], []) qbit = self.dag.qubits()[0] self.assertEqual([1, 11, 12, 2], [i._node_id for i in self.dag.nodes_on_wire(qbit)]) self.assertEqual( [11, 12], [i._node_id for i in self.dag.nodes_on_wire(qbit, only_ops=True)]) cbit = self.dag.clbits()[0] self.assertEqual([7, 8], [i._node_id for i in self.dag.nodes_on_wire(cbit)]) self.assertEqual( [], [i._node_id for i in self.dag.nodes_on_wire(cbit, only_ops=True)]) with self.assertRaises(DAGCircuitError): next(self.dag.nodes_on_wire((qbit.register, 7)))