From the 'Basic Quantum Operations' tutorial:
# Initializing a three-qubit quantum state
import math
desired_vector = [
1 / math.sqrt(16) * complex(0, 1),
1 / math.sqrt(8) * complex(1, 0),
1 / math.sqrt(16) * complex(1, 1),
0,
0,
1 / math.sqrt(8) * complex(1, 2),
1 / math.sqrt(16) * complex(1, 0),
0]
q = QuantumRegister(3)
qc = QuantumCircuit(q)
qc.initialize(desired_vector, [q[0],q[1],q[2]])
circuit_drawer(qc)
gives
DAGCircuitError: 'init is not in the list of basis operations'
from the call to circuit_drawer
Run the 'Basic Quantum Operations' tutorial, or the above code from that notebook.
This is because qc.initialize is a composite gate, but the circuit_drawer calls the DAG generation routine as DAGCircuit.fromQuantumCircuit(circuit, expand_gates=False), thus throwing an error becauseinitialize (called 'init') is not in the basis.
This is more reason the drawer should not use the unroller and just draw from the circuit.
Totally agree with @jaygambetta
@nonhermitian, calling fromQuantumCircuit() differently solves the issue?
Removing expand_gates=False fixes this issue. It seems that the 0.5.7 drawer automatically expanded to the basis set. So this would be a temp fix. However, to do what @jaygambetta wants, requires more work.
Not passing expand_gates=False has two main issues. First, it re-introduces #732 and the following code will break:
from qiskit import *
from qiskit.tools.visualization import plot_circuit
from qiskit.extensions import simulator
qr = QuantumRegister(1)
qc = QuantumCircuit(qr)
qc.sdg(qr[0])
qc.snapshot('0')
plot_circuit(qc)
It breaks indicating somehow that it cannot expand snapshot nor find it inside the opaque declarations:
File "/Users/salva/workspace/qiskit-sdk-py/qiskit/unroll/_dagunroller.py", line 86, in expand_gates
not self.dag_circuit.gates[current_node["name"]]["opaque"]:
KeyError: 'snapshot'
The extension mechanism of Qiskit circuits is flawed as it is reported in https://github.com/Qiskit/qiskit-terra/issues/732#issuecomment-413505414 and https://github.com/Qiskit/qiskit-terra/issues/905.
The second problem is that, if the unroller is expanding the gates, you are no longer plotting the circuit you designed but an equivalent one, losing some representation accuracy. A simple example is that the following code:
from qiskit import *
from qiskit.tools.visualization import plot_circuit
qr = QuantumRegister(1)
qc = QuantumCircuit(qr)
qc.sdg(qr[0])
plot_circuit(qc)
Produces:

But allowing the expansion of gates makes the same code to produce:

We should find a solution that deals with both problems.
Well, one of our current core tutorials is broken because of this. @jaygambetta do you want me to modify the tutorial notebook?
@nonhermitian can you just modify the notebook for now to remove visualization of circuit containing initialize? We need to modify the drawer to not unroll, and to also handle custom gate drawing. This will take a little bit longer.
Ok
@muneerqu Thanks for referencing #330. Staying tuned.
Shall we split this into two issues @ajavadia ?
We are close to supporting custom gates in the DAG. From there, we need to make some adjustments to each drawer, and then hopefully we will be able to draw custom gates in circuits.
This issue has been raised a number of times (#1230, #1251), so it's high priority.
I just tested this again since we moved to using the dag directly for circuit drawing in the last release and I am not able to reproduce the visualization failure anymore (from the code in the description). @ajavadia was there something else we needed to do here for this issue?
I'm closing this bug because drawing an initial vector works fine now and has since we migrated to using the dag for visualization prior to 0.7. I've also updated the tutorial notebook to add back the previously removed drawing call: https://github.com/Qiskit/qiskit-tutorials/pull/533
Most helpful comment
This is more reason the drawer should not use the unroller and just draw from the circuit.