Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Added
- Added function for purity of a mixed state in ``qiskit.quantum_information``
(#1733)
- Added parameter to the TextProgressBar to allow the output to be sent to a
different output stream
different output stream
- Added a ``__qiskit_version__`` parameter to the qiskit namespace. This will
contain a dictionary of versions for all installed qiskit elements. (#1885).
- Added a ``RunConfig`` object for configurations related to running an
Expand All @@ -50,6 +50,8 @@ Added
- ``execute_circuits()`` and ``assemble_circuits()`` allow setting a qobj_header of type
QobjHeader to add extra information to the qobj (and thus result).
- Register indexing supports negative indices (#1875)
- Added ``nodes_on_wire()`` to DAGCircuit which returns an iterator over all the
operations on the given wire

Changed
-------
Expand Down
2 changes: 1 addition & 1 deletion qiskit/converters/circuit_to_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


from qiskit.circuit.compositegate import CompositeGate
from qiskit.dagcircuit._dagcircuit import DAGCircuit
from qiskit.dagcircuit.dagcircuit import DAGCircuit


def circuit_to_dag(circuit):
Expand Down
4 changes: 2 additions & 2 deletions qiskit/dagcircuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
# the LICENSE.txt file in the root directory of this source tree.

"""Module for DAG Circuits."""
from ._dagcircuit import DAGCircuit
from ._dagnode import DAGNode
from .dagcircuit import DAGCircuit
from .dagnode import DAGNode
from .exceptions import DAGCircuitError
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from qiskit.circuit.classicalregister import ClassicalRegister
from qiskit.circuit.gate import Gate
from .exceptions import DAGCircuitError
from ._dagnode import DAGNode
from .dagnode import DAGNode


class DAGCircuit:
Expand Down Expand Up @@ -1458,6 +1458,41 @@ def collect_runs(self, namelist):
group_list.append(tuple(group))
return set(group_list)

def nodes_on_wire(self, wire, only_ops=False):
"""
Iterator for nodes that affect a given wire

Args:
wire (tuple(Register, index)): the wire to be looked at.
only_ops (bool): True if only the ops nodes are wanted
otherwise all nodes are returned.
Yield:
DAGNode: the successive ops on the given wire

Raises:
DAGCircuitError: if the given wire doesn't exist in the DAG
"""
current_node = self.input_map.get(wire, None)

if not current_node:
raise DAGCircuitError('The given wire %s is not present in the circuit'
% str(wire))

more_nodes = True
Comment thread
maddy-tod marked this conversation as resolved.
Outdated
while more_nodes:
more_nodes = False
# allow user to just get ops on the wire - not the input/output nodes
if current_node.type == 'op' or not only_ops:
yield current_node

for node in self.successors(current_node):
# check if this node includes the given wire
if (node.type in ['in', 'out'] and wire == node.wire) or \
(node.type == 'op' and wire in node.qargs + node.cargs):
current_node = node
more_nodes = True
break

def count_ops(self):
"""Count the occurrences of operation names.

Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions test/python/test_dagcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,25 @@ def test_nodes_in_topological_order(self):
('cr[1]', [])]
self.assertEqual(expected, [(i.name, i.qargs) for i in named_nodes])

def test_dag_ops_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.qubit0, self.qubit1])
self.dag.apply_operation_back(HGate(self.qubit0), [self.qubit0])

qbit = self.dag.qubits()[0]
self.assertEqual([1, 11, 12, 2], list(self.dag.nodes_on_wire(qbit)))
self.assertEqual([11, 12], list(self.dag.nodes_on_wire(qbit, only_ops=True)))

cbit = self.dag.clbits()[0]
self.assertEqual([7, 8], list(self.dag.nodes_on_wire(cbit)))
self.assertEqual([], list(self.dag.nodes_on_wire(cbit, only_ops=True)))

(reg, _) = qbit
with self.assertRaises(DAGCircuitError):
next(self.dag.nodes_on_wire((reg, 7)))


class TestDagLayers(QiskitTestCase):
"""Test finding layers on the dag"""
Expand Down