def test_can_append_to_quantum_circuit(self): """Test that we can add various objects with Operation interface to a Quantum Circuit.""" qc = QuantumCircuit(6, 1) qc.append(XGate(), [2]) qc.append(Barrier(3), [1, 2, 4]) qc.append(CXGate(), [0, 1]) qc.append(Measure(), [1], [0]) qc.append(Reset(), [0]) qc.cx(3, 4) qc.append(Gate("some_gate", 3, []), [1, 2, 3]) qc.append(Initialize([0.5, 0.5, 0.5, 0.5]), [4, 5]) qc.append(Isometry(np.eye(4, 4), 0, 0), [3, 4]) qc.append(Pauli("II"), [0, 1]) # Appending Clifford circ1 = QuantumCircuit(2) circ1.h(1) circ1.cx(0, 1) qc.append(Clifford(circ1), [0, 1]) # Appending CNOTDihedral circ2 = QuantumCircuit(2) circ2.t(0) circ2.x(0) circ2.t(1) qc.append(CNOTDihedral(circ2), [2, 3]) # If we got to here, we have successfully appended everything to qc self.assertIsInstance(qc, QuantumCircuit)
def test_barrier_as_operation(self): """Test that we can instantiate an object of class :class:`~qiskit.circuit.Barrier` and that it has the expected name, num_qubits and num_clbits. """ num_qubits = 4 op = Barrier(num_qubits) self.assertTrue(op.name == "barrier") self.assertTrue(op.num_qubits == num_qubits) self.assertTrue(op.num_clbits == 0)
def run(self, dag): new_dag = DAGCircuit() for qreg in dag.qregs.values(): new_dag.add_qreg(qreg) for creg in dag.cregs.values(): new_dag.add_creg(creg) for node in dag.op_nodes(): new_dag.apply_operation_back(node.op, node.qargs, node.cargs) logger.info('SequentialPass: adding node {node.name}') if node.name in ['barrier', 'measure']: continue new_dag.apply_operation_back(Barrier(new_dag.num_qubits()), list(new_dag.qubits), []) return new_dag
def run(self, dag): new_dag = DAGCircuit() for qreg in dag.qregs.values(): new_dag.add_qreg(qreg) for creg in dag.cregs.values(): new_dag.add_creg(creg) for ii, layer in enumerate(dag.layers()): gates_1q = [] gates_2q = [] other_gates = [] for node in layer['graph'].op_nodes(): if len(node.qargs) == 2: gates_2q.append(node) elif len(node.qargs) == 1: gates_1q.append(node) else: logging.info(f'layer {ii}: other type of node {node}') other_gates.append(node) even = [] odd = [] for node in gates_1q: if node.qargs[0].index % 2 == 0: even.append(node) else: odd.append(node) logging.info( f'layer {ii}: 2q gates {len(gates_2q)}, even {len(even)} odd {len(odd)}, other {len(other_gates)}' ) if len(even) > 0: for node in even: new_dag.apply_operation_back(node.op, node.qargs, node.cargs) if not isinstance(node.op, Barrier): new_dag.apply_operation_back(Barrier(new_dag.num_qubits()), list(new_dag.qubits), []) if len(odd) > 0: for node in odd: new_dag.apply_operation_back(node.op, node.qargs, node.cargs) if not isinstance(node.op, Barrier): new_dag.apply_operation_back(Barrier(new_dag.num_qubits()), list(new_dag.qubits), []) for node in gates_2q: new_dag.apply_operation_back(node.op, node.qargs, node.cargs) if not isinstance(node.op, Barrier): new_dag.apply_operation_back(Barrier(new_dag.num_qubits()), list(new_dag.qubits), []) for node in other_gates: new_dag.apply_operation_back(node.op, node.qargs, node.cargs) if not isinstance(node.op, Barrier): new_dag.apply_operation_back(Barrier(new_dag.num_qubits()), list(new_dag.qubits), []) return new_dag
def append_tk_command_to_qiskit( op: "Op", args: List["UnitID"], qcirc: QuantumCircuit, qregmap: Dict[str, QuantumRegister], cregmap: Dict[str, ClassicalRegister], symb_map: Dict[Parameter, sympy.Symbol], range_preds: Dict[Bit, Tuple[List["UnitID"], int]], ) -> Instruction: optype = op.type if optype == OpType.Measure: qubit = args[0] bit = args[1] qb = qregmap[qubit.reg_name][qubit.index[0]] b = cregmap[bit.reg_name][bit.index[0]] return qcirc.measure(qb, b) if optype == OpType.Reset: qb = qregmap[args[0].reg_name][args[0].index[0]] return qcirc.reset(qb) if optype in [ OpType.CircBox, OpType.ExpBox, OpType.PauliExpBox, OpType.Custom ]: subcircuit = op.get_circuit() subqc = tk_to_qiskit(subcircuit) qargs = [] cargs = [] for a in args: if a.type == UnitType.qubit: qargs.append(qregmap[a.reg_name][a.index[0]]) else: cargs.append(cregmap[a.reg_name][a.index[0]]) if optype == OpType.Custom: instruc = subqc.to_gate() instruc.name = op.get_name() else: instruc = subqc.to_instruction() return qcirc.append(instruc, qargs, cargs) if optype == OpType.Unitary2qBox: qargs = [qregmap[q.reg_name][q.index[0]] for q in args] u = op.get_matrix() g = UnitaryGate(u, label="u2q") return qcirc.append(g, qargs=qargs) if optype == OpType.Barrier: qargs = [qregmap[q.reg_name][q.index[0]] for q in args] g = Barrier(len(args)) return qcirc.append(g, qargs=qargs) if optype == OpType.RangePredicate: if op.lower != op.upper: raise NotImplementedError range_preds[args[-1]] = (args[:-1], op.lower) # attach predicate to bit, # subsequent conditional will handle it return Instruction("", 0, 0, []) if optype == OpType.ConditionalGate: if args[0] in range_preds: assert op.value == 1 condition_bits, value = range_preds[args[0]] del range_preds[args[0]] args = condition_bits + args[1:] width = len(condition_bits) else: width = op.width value = op.value regname = args[0].reg_name if len(cregmap[regname]) != width: raise NotImplementedError( "OpenQASM conditions must be an entire register") for i, a in enumerate(args[:width]): if a.reg_name != regname: raise NotImplementedError( "OpenQASM conditions can only use a single register") if a.index != [i]: raise NotImplementedError( "OpenQASM conditions must be an entire register in order") instruction = append_tk_command_to_qiskit(op.op, args[width:], qcirc, qregmap, cregmap, symb_map, range_preds) instruction.c_if(cregmap[regname], value) return instruction # normal gates qargs = [qregmap[q.reg_name][q.index[0]] for q in args] if optype == OpType.CnX: return qcirc.mcx(qargs[:-1], qargs[-1]) # special case if optype == OpType.CnRy: # might as well do a bit more checking assert len(op.params) == 1 alpha = param_to_qiskit(op.params[0], symb_map) assert len(qargs) >= 2 if len(qargs) == 2: # presumably more efficient; single control only new_gate = CRYGate(alpha) else: new_ry_gate = RYGate(alpha) new_gate = MCMT(gate=new_ry_gate, num_ctrl_qubits=len(qargs) - 1, num_target_qubits=1) qcirc.append(new_gate, qargs) return qcirc # others are direct translations try: gatetype = _known_qiskit_gate_rev[optype] except KeyError as error: raise NotImplementedError("Cannot convert tket Op to Qiskit gate: " + op.get_name()) from error params = [param_to_qiskit(p, symb_map) for p in op.params] g = gatetype(*params) return qcirc.append(g, qargs=qargs)