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] x_gate = XGate() x_gate.condition = (creg, 3) dag = DAGCircuit() dag.add_qreg(qreg) dag.add_creg(creg) dag.apply_operation_back(HGate(), [qubit0], []) dag.apply_operation_back(CXGate(), [qubit0, qubit1], []) dag.apply_operation_back(Measure(), [qubit1, clbit1], []) dag.apply_operation_back(x_gate, [qubit1], []) dag.apply_operation_back(Measure(), [qubit0, clbit0], []) dag.apply_operation_back(Measure(), [qubit1, clbit1], []) 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 test_apply_operation_back(self): """The apply_operation_back() method.""" x_gate = XGate() x_gate.condition = self.condition self.dag.apply_operation_back(HGate(), [self.qubit0], []) self.dag.apply_operation_back(CXGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(Measure(), [self.qubit1, self.clbit1], []) self.dag.apply_operation_back(x_gate, [self.qubit1], []) self.dag.apply_operation_back(Measure(), [self.qubit0, self.clbit0], []) self.dag.apply_operation_back(Measure(), [self.qubit1, self.clbit1], []) self.assertEqual(len(list(self.dag.nodes())), 16) self.assertEqual(len(list(self.dag.edges())), 17)
def test_edges(self): """Test that DAGCircuit.edges() behaves as expected with ops.""" x_gate = XGate() x_gate.condition = self.condition self.dag.apply_operation_back(HGate(), [self.qubit0], []) self.dag.apply_operation_back(CXGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(Measure(), [self.qubit1, self.clbit1], []) self.dag.apply_operation_back(x_gate, [self.qubit1], []) self.dag.apply_operation_back(Measure(), [self.qubit0, self.clbit0], []) self.dag.apply_operation_back(Measure(), [self.qubit1, self.clbit1], []) out_edges = self.dag.edges(self.dag.output_map.values()) self.assertEqual(list(out_edges), []) in_edges = self.dag.edges(self.dag.input_map.values()) # number of edges for input nodes should be the same as number of wires self.assertEqual(len(list(in_edges)), 5)