1
$\begingroup$

enter image description here

i want DAGCircuit like this

enter image description here

The Following is my Source code

from qiskit.dagcircuit import DAGCircuit

from qiskit.circuit import QuantumCircuit, QuantumRegister

from qiskit.converters import circuit_to_dag

from qiskit.visualization import dag_drawer

from qiskit.circuit.library.standard_gates import CZGate

# Create a QuantumCircuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
print(qc)

# Convert the circuit to a DAGCircuit
dag = circuit_to_dag(qc)

# Get the source qubits
source_qubits = []
for qubit in dag.qubits:
    if dag.input_map[qubit]:
        source_qubits.append(qubit)

# Print the source qubits
#print("Source qubits:", source_qubits)

# Get the sink qubits
sink_qubits = []
for qubit in dag.qubits:
    if dag.output_map[qubit]:
        sink_qubits.append(qubit)

# Print the sink qubits
#print("Sink qubits:", sink_qubits)

# Add new qubits and gates
qreg_sr = QuantumRegister(1, name='Source')
qreg_sk = QuantumRegister(1, name='Sink')
cz = CZGate()

#for qubit in source_qubits:
dag.apply_operation_front(qreg_sr, source_qubits )
dag.apply_operation_back(qreg_sk, sink_qubits)


dag_drawer(dag)

i get the DAGCircuit enter image description here

i Want that Source node is on top of Green q[0] , q1 and sink node bellow the red q[0], q1.

$\endgroup$
3
  • $\begingroup$ if I Change #for qubit in source_qubits: dag.apply_operation_front(qreg_sr, source_qubits ) dag.apply_operation_back(qreg_sk, sink_qubits) into bellow changes dag.apply_operation_front(source_qubits[0].register,qreg_sr ) dag.apply_operation_back(sink_qubits[0].register,qreg_sk) then i get bellow error DAGCircuitError: "(qu)bit Qubit(QuantumRegister(1, 'Source'), 0) not found in OrderedDict([(Qubit(QuantumRegister(2, 'q'), 0), DAGInNode(wire=Qubit(QuantumRegister(2, 'q'), 0))), (Qubit(QuantumRegister(2, 'q'), 1), DAGInNode(wire=Qubit(QuantumRegister(2, 'q'), 1)))])" $\endgroup$ Commented Mar 28, 2023 at 19:19
  • $\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$
    – Community Bot
    Commented Mar 29, 2023 at 10:23
  • $\begingroup$ I want to add single source before i.e on top of green qubits q[0],q[1] and single sink after the red qubits q[0], q[1] bellow. $\endgroup$ Commented Mar 29, 2023 at 18:24

1 Answer 1

1
$\begingroup$

Having a source and sink node like that does not fit into the data model of a DAGCircuit object. In a DAGCircuit the first layer of the DAG needs to be the input nodes and the last layer needs to be the output nodes, this is to track the data flow between operations and is fundamental to how it is used. So the DAGCircuit class's API will not let you create a graph like that because it would break the expectations when using a DAGCircuit object.

That being said if you're ok with not using the DAGCircuit API after you make the transformation you can manually copy the internal rustworkx graph and modify it as you want. For example:

new_graph = dag._multi_graph.copy()
source_node_id = new_graph.add_node('source')
sink_node_id = new_graph.add_node('sink')
source_edges = [(source_node_id, node._node_id) for node in dag.input_map.values()]
new_graph.add_edges_from_no_data(source_edges)
sink_edges = [(node._node_id, sink_node_id) for node in dag.output_map.values()]
new_graph.add_edges_from_no_data(sink_edges)

new_graph there will be a rustworkx PyDiGraph object that contains the graph you're looking for. However, you won't be able to use it with any of the DAGCircuit API as that graph is no longer a representation of a DAGCircuit Also, as the code example shows you're accessing explicitly private/internal interfaces in DAGCircuit to do this, so it may not work in future versions of Qiskit.

$\endgroup$
2
  • $\begingroup$ can u help in the above code to get desired DAGCircuit $\endgroup$ Commented Mar 28, 2023 at 18:57
  • $\begingroup$ As I said in the first paragraph of my response the graph structure you're looking for is not a DAGCircuit. What you're asking for is incompatible with the data model a DAGCircuit object expects and the DAGCircuit API will not let you create it as it breaks the expectations of the class. The code sample in the answer above is about the closest you can do, which is a DAG as a graph object based on a copy of the internal graph object used internally by DAGCircuit. $\endgroup$ Commented Mar 28, 2023 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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