0
$\begingroup$

I have the following code, which runs properly.

from qiskit import *
from qiskit.converters import *

ghz2 = QuantumCircuit(5, 5)
ghz2.h(2)
ghz2.cx(2, 1)
ghz2.cx(1, 0)
ghz2.cx(2, 3)
ghz2.cx(3, 4)


ghz_dag = circuit_to_dag(ghz2)

How do I access properties of this dag? For instance, I would like to get all the 2-qubit gates using the collect 2qubit gates method (the output is supposed to be a dictionary) or simply draw the DAG but again, dag_drawer(ghz_dag) throws up an error ('MultiDiGraph' object has no attribute 'number_of_selfloops') even though, that's done in an example here.

Maybe this is just a Python usage question but how does one access the properties of the DAG?

$\endgroup$
  • $\begingroup$ Hi, which versions of qiskit are you running? I am unable to recreate this $\endgroup$ – met927 Nov 18 at 11:53
  • $\begingroup$ @met927 if I type conda list, I see 0.13.0 under the version for qiskit. I am running the commands from the question in the Spyder IPython console after restarting the kernel. $\endgroup$ – user1936752 Nov 18 at 12:02
  • $\begingroup$ Ah ok, that is good. Which version of networkx are you running? $\endgroup$ – met927 Nov 18 at 13:24
  • $\begingroup$ @met927 I'm using 2.4. Just to add, it's not only the specific dag_drawer that I'm having trouble with - it'd be good to know generally how to access these various properties of the dag. Thank you $\endgroup$ – user1936752 Nov 18 at 14:06
2
$\begingroup$

Properties of the DAG are simply accessed using dot notation. For example, if you would like to get all the two qubit gates you would do dag.twoQ_gates() which returns a list of the two qubit gates present in the dag.

The code you have linked to is a transpiler pass. This is a method that looks over the dag to identify some property or to perform some optimization. It stores variables to the property set, self.property_set. There is more information about using the transpiler here.

$\endgroup$
  • $\begingroup$ When I run, for example, ghz_dag.twoQ_gates(), I see the following output [<qiskit.dagcircuit.dagnode.DAGNode at 0x1f5aa0ddc08>, <qiskit.dagcircuit.dagnode.DAGNode at 0x1f5aa0ddc88>, <qiskit.dagcircuit.dagnode.DAGNode at 0x1f5aa0dd808>, <qiskit.dagcircuit.dagnode.DAGNode at 0x1f5aa0ddf08>]. How do I see this as a list or a dictionary where I can read off the gate and what qubits it acts on? $\endgroup$ – user1936752 Nov 18 at 17:48
  • $\begingroup$ This is a list, of type DAGNode. From the DAGNodes you can read all the properties of the node, for example node.op will get you the operation that corresponds to this node. You can see the whole class here $\endgroup$ – met927 Nov 18 at 18:14
  • $\begingroup$ Thank you - that was very useful! I assume dag_drawer doesn't work because of networkx? In any case, these methods are very helpful! $\endgroup$ – user1936752 Nov 18 at 19:37
  • $\begingroup$ Happy to help! Yes I think it might be a networkx issue $\endgroup$ – met927 Nov 19 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.