def test_add_qreg_creg(self): """ add_qreg() and add_creg() methods""" dag = DAGCircuit() dag.add_qreg(QuantumRegister(2, 'qr')) dag.add_creg(ClassicalRegister(1, 'cr')) self.assertDictEqual(dag.qregs, {'qr': QuantumRegister(2, 'qr')}) self.assertDictEqual(dag.cregs, {'cr': ClassicalRegister(1, 'cr')})
def test_create(self): """ Creation using add_basis_element(), add_qreg(), add_creg(), and apply_operation_back().""" qreg = QuantumRegister(2, 'qr') creg = ClassicalRegister(2, 'cr') qubit0 = ('qr', 0) qubit1 = ('qr', 1) clbit0 = ('cr', 0) clbit1 = ('cr', 1) condition = ('cr', 3) dag = DAGCircuit() dag.add_basis_element('h', 1, number_classical=0, number_parameters=0) dag.add_basis_element('cx', 2) dag.add_basis_element('x', 1) dag.add_basis_element('measure', 1, number_classical=1, number_parameters=0) dag.add_qreg(qreg) dag.add_creg(creg) dag.apply_operation_back('h', [qubit0], [], [], condition=None) dag.apply_operation_back('cx', [qubit0, qubit1], [], [], condition=None) dag.apply_operation_back('measure', [qubit1], [clbit1], [], condition=None) dag.apply_operation_back('x', [qubit1], [], [], condition=condition) dag.apply_operation_back('measure', [qubit0], [clbit0], [], condition=None) dag.apply_operation_back('measure', [qubit1], [clbit1], [], condition=None) self.assertEqual(len(dag.multi_graph.nodes), 14) self.assertEqual(len(dag.multi_graph.edges), 16)
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.dag.add_basis_element(name='h', number_qubits=1, number_classical=0, number_parameters=0) self.dag.add_basis_element('cx', 2, 0, 0) self.dag.add_basis_element('x', 1, 0, 0) self.dag.add_basis_element('measure', 1, 1, 0) self.dag.add_basis_element('reset', 1, 0, 0) self.qubit0 = qreg[0] self.qubit1 = qreg[1] self.qubit2 = qreg[2] self.clbit0 = creg[0] self.clbit1 = creg[1] self.condition = (creg, 3)
def test_layers_basic(self): """ The layers() method.""" qreg = QuantumRegister(2, 'qr') creg = ClassicalRegister(2, 'cr') qubit0 = ('qr', 0) qubit1 = ('qr', 1) clbit0 = ('cr', 0) clbit1 = ('cr', 1) condition = ('cr', 3) dag = DAGCircuit() dag.add_basis_element('h', 1, number_classical=0, number_parameters=0) dag.add_basis_element('cx', 2) dag.add_basis_element('x', 1) dag.add_basis_element('measure', 1, number_classical=1, number_parameters=0) dag.add_qreg(qreg) dag.add_creg(creg) dag.apply_operation_back('h', [qubit0], [], [], condition=None) dag.apply_operation_back('cx', [qubit0, qubit1], [], [], condition=None) dag.apply_operation_back('measure', [qubit1], [clbit1], [], condition=None) dag.apply_operation_back('x', [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[1]["name"] for node in layer["graph"].multi_graph.nodes(data=True) if node[1]["type"] == "op" ] for layer in layers] self.assertEqual( [['h'], ['cx'], ['measure'], ['x'], ['measure', 'measure']], name_layers)
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_basis_element('h', 1, 0, 0) dag.add_basis_element('cx', 2, 0, 0) dag.add_basis_element('x', 1, 0, 0) dag.add_basis_element('measure', 1, 1, 0) 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[1]["op"].name for node in layer["graph"].multi_graph.nodes(data=True) if node[1]["type"] == "op"] for layer in layers] self.assertEqual([ ['h'], ['cx'], ['measure'], ['x'], ['measure', 'measure'] ], name_layers)
def _process_node(self, node): """Carry out the action associated with node n.""" if node.type == "program": self._process_children(node) elif node.type == "qreg": qreg = QuantumRegister(node.index, node.name) self.qregs[node.name] = qreg self.backend.new_qreg(qreg) elif node.type == "creg": creg = ClassicalRegister(node.index, node.name) self.cregs[node.name] = creg self.backend.new_creg(creg) elif node.type == "id": raise UnrollerError("internal error: _process_node on id") elif node.type == "int": raise UnrollerError("internal error: _process_node on int") elif node.type == "real": raise UnrollerError("internal error: _process_node on real") elif node.type == "indexed_id": raise UnrollerError("internal error: _process_node on indexed_id") elif node.type == "id_list": # We process id_list nodes when they are leaves of barriers. return [ self._process_bit_id(node_children) for node_children in node.children ] elif node.type == "primary_list": # We should only be called for a barrier. return [self._process_bit_id(m) for m in node.children] elif node.type == "gate": self._process_gate(node) elif node.type == "custom_unitary": self._process_custom_unitary(node) elif node.type == "universal_unitary": args = self._process_node(node.children[0]) qid = self._process_bit_id(node.children[1]) for element in qid: self.backend.u(args, element) elif node.type == "cnot": self._process_cnot(node) elif node.type == "expression_list": return node.children elif node.type == "binop": raise UnrollerError("internal error: _process_node on binop") elif node.type == "prefix": raise UnrollerError("internal error: _process_node on prefix") elif node.type == "measure": self._process_measure(node) elif node.type == "format": self.version = node.version() self.backend.version(node.version()) elif node.type == "barrier": ids = self._process_node(node.children[0]) self.backend.barrier(ids) elif node.type == "reset": id0 = self._process_bit_id(node.children[0]) for i, _ in enumerate(id0): self.backend.reset(id0[i]) elif node.type == "if": self._process_if(node) elif node.type == "opaque": self._process_gate(node, opaque=True) elif node.type == "external": raise UnrollerError("internal error: _process_node on external") else: raise UnrollerError("internal error: undefined node type", node.type, "line=%s" % node.line, "file=%s" % node.file) return None
def test_add_reg_bad_type(self): """ add_qreg with a classical register is not allowed.""" dag = DAGCircuit() cr = ClassicalRegister(2) self.assertRaises(DAGCircuitError, dag.add_qreg, cr)