# Any way to draw my quantum circuit?

**URL:** <https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290>\
**Category:** PennyLane Plugins\
**Created:** [December 6, 2019, 7:34am UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290 "2019-12-06T07:34:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leeseok\_Kim](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/leeseok_kim/32/106_2.png) [@Leeseok\_Kim](https://discuss.pennylane.ai/u/Leeseok_Kim)\
**Post date:** [December 6, 2019, 7:34am UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/1 "2019-12-06T07:34:04Z")

</div>

Hi! I was wondering if there’s any way to print out my circuit so that my circuit is same as what I was meant to design. Since there is no insert strategy for gates (such as the ones in Cirq), I am not sure whether my quantum circuit is designed in the way I was thinking.

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [December 6, 2019, 12:31pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/2 "2019-12-06T12:31:06Z")

</div>

Hi @Leeseok_Kim! You can use the [`QNode.print_applied()`](https://pennylane.readthedocs.io/en/stable/code/api/pennylane.QNode.html?highlight=print%20applied#pennylane.QNode.print_applied) method to print out the gates applied by the QNode after each evaluation.

We’re also currently working on adding a circuit drawing feature as well 🙂

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [December 6, 2019, 12:36pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/3 "2019-12-06T12:36:02Z")

</div>

For example:

```python
dev = qml.device('default.qubit', wires=2)

@qml.qnode(dev)
def circuit(x):
    qml.RZ(x, wires=0)
    qml.CNOT(wires=[0,1])
    qml.RY(x, wires=1)
    return qml.expval(qml.PauliZ(1))

circuit(0.543)
circuit.print_applied()

```

will give the output

```auto
Operations
----------
RZ(0.543, wires=0)
CNOT(wires=[0,1])
RY(0.543, wires=1)

Observables
-----------
expval(PauliZ(wires=1))

```

---

<div class="post-metadata">

**Author:** ![Leeseok\_Kim](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/leeseok_kim/32/106_2.png) [@Leeseok\_Kim](https://discuss.pennylane.ai/u/Leeseok_Kim)\
**Post date:** [December 6, 2019, 9:22pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/4 "2019-12-06T21:22:41Z")

</div>

Hi- thanks! Also, I have a few questions on designing quantum circuits. Is there any way that I can contact with you in easier/faster way other than this forum? If there’s not, this forum would be fine with me.

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [December 7, 2019, 1:02am UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/5 "2019-12-07T01:02:15Z")

</div>

This forum would likely be the faster way, since it is checked by various PennyLane developers — so if I don’t see your reply straight away, someone else would be able to reply in the meantime 🙂

In addition, you can also join our Slack channel: the link is [Slack](https://u.strawberryfields.ai/slack)

To follow on from your original question, you can also directly access the Cirq circuit object; it is available via `dev.circuit`. With this object, you can perform standard Cirq circuit drawing.

---

<div class="post-metadata">

**Author:** ![angelinaG](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/angelinag/32/148_2.png) [@angelinaG](https://discuss.pennylane.ai/u/angelinaG)\
**Post date:** [January 30, 2020, 10:43am UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/6 "2020-01-30T10:43:43Z")

</div>

Hi @Leeseok_Kim!  
Well, I am a novice in quantum machine learning and you may already be familiar with what I am mentioning, but since it was an interesting observation, I thought of sharing it here. While working on the paper by (Mari et al., 2019), I was curious to plot the circuit graphically. I tried to sketch it manually on understanding the algorithm, but when I connected to the actual IBMQ quantum hardware to execute the circuit, it was interesting to obtain a colourful circuit plot from the IBMQ website (refer to the results dashboard). One can easily save the plot from there.

---

<div class="post-metadata">

**Author:** ![johannesjmeyer](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/johannesjmeyer/32/115_2.png) [@johannesjmeyer](https://discuss.pennylane.ai/u/johannesjmeyer)\
**Post date:** [January 30, 2020, 1:56pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/7 "2020-01-30T13:56:39Z")

</div>

Hi @Leeseok_Kim and @angelinaG,

the upcoming version of PennyLane will include a circuit drawer comparable to the one included in Cirq. If you want to try it before the new version is released, you can install PennyLane from the current master branch on Github via

```auto
pip install git+https://www.github.com/XanaduAI/pennylane

```

Assume you already defined a QNode named `circuit`. The circuit drawer is then used in the following way:

```python
>>> result = circuit(args)
>>> print(circuit.draw())
0: ──H──╭C───────────────────────────╭C─────────╭┤ ⟨Z ⊗ Z⟩
1: ─────╰RX(2.3)──Rot(1.2, 3.2, 0.7)──╰RX(-2.3)──╰┤ ⟨Z ⊗ Z⟩

```

The format is somewhat skewed in this forum, but it will look good in the console!

---

<div class="post-metadata">

**Author:** ![angelinaG](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/angelinag/32/148_2.png) [@angelinaG](https://discuss.pennylane.ai/u/angelinaG)\
**Post date:** [January 30, 2020, 4:59pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/8 "2020-01-30T16:59:56Z")

</div>

This is very interesting to note @johannesjmeyer!  
Thank you very much!  
I shall try this out! 👍

---

<div class="post-metadata">

**Author:** ![alanspace](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alanspace/32/320_2.png) [@alanspace](https://discuss.pennylane.ai/u/alanspace)\
**Post date:** [September 25, 2020, 12:37pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/9 "2020-09-25T12:37:31Z")

</div>

Hello, thanks for the above suggestion. I want to ask further that can we print circuit.draw(output=‘mpl’) sth like Qiskit so we can know how the operation looks like for multiple qubits?  
Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Tom\_Bromley](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tom_bromley/32/129_2.png) [@Tom\_Bromley](https://discuss.pennylane.ai/u/Tom_Bromley)\
**Post date:** [September 25, 2020, 5:38pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/11 "2020-09-25T17:38:03Z")

</div>

Hey @alanspace!

Unfortunately PennyLane does not currently support circuit visualization using Matplotlib. It is definitely something on our roadmap, but for now I recommend using the [existing](https://pennylane.readthedocs.io/en/stable/development/release_notes.html#release-0-8-0) text-based drawer.

Thanks!

---

<div class="post-metadata">

**Author:** ![Amandeep](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/amandeep/32/385_2.png) [@Amandeep](https://discuss.pennylane.ai/u/Amandeep)\
**Post date:** [October 22, 2020, 4:35am UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/12 "2020-10-22T04:35:55Z")

</div>

Can we draw and visualize colorful quantum circuits now in latest version of PennyLane?

---

<div class="post-metadata">

**Author:** ![Tom\_Bromley](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tom_bromley/32/129_2.png) [@Tom\_Bromley](https://discuss.pennylane.ai/u/Tom_Bromley)\
**Post date:** [October 22, 2020, 11:50am UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/13 "2020-10-22T11:50:58Z")

</div>

Hi @Amandeep,

Unfortunately this feature is not yet available, but definitely something that is on our radar.

For now, text-based circuit drawing is available through [circuit.draw()](https://pennylane.readthedocs.io/en/stable/introduction/circuits.html#creating-a-quantum-node).

---

<div class="post-metadata">

**Author:** ![milanleonard](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/milanleonard/32/318_2.png) [@milanleonard](https://discuss.pennylane.ai/u/milanleonard)\
**Post date:** [November 1, 2020, 4:13am UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/14 "2020-11-01T04:13:23Z")

</div>

Sorry to bring this bring this back up, but given that there must be a way to convert to qiskit within the pennylane-qiskit plugin, perhaps a wrapper for the qiskit draw could be trivially provided. I can’t seem to find the to\_qiskit function that I assume must exists.

I find I’m often rewriting my quantum circuit code in qiskit just to get the plotting capability.

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [November 1, 2020, 2:18pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/15 "2020-11-01T14:18:14Z")

</div>

Hi @milanleonard!

If you are using the a Qiskit device with PennyLane, it is possible to access the Qiskit program, and thus the Qiskit drawer, via the device:

```python
>>> dev = qml.device("qiskit.aer", wires=2)
>>> @qml.qnode(dev)
... def circuit(x):
... qml.RX(0.5, wires=0)
... qml.CNOT(wires=[0, 1])
... return qml.expval(qml.PauliZ(0))
>>> circuit(0.5)
0.861328125
>>> dev._circuit.draw()
     ┌─────────┐ ┌─┐
q_0: ┤ RX(0.5) ├──■──┤M├───
     └─────────┘┌─┴─┐└╥┘┌─┐
q_1: ───────────┤ X ├─╫─┤M├
                └───┘ ║ └╥┘
c: 2/═════════════════╩══╩═
                      0 1 

```

---

<div class="post-metadata">

**Author:** ![milanleonard](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/milanleonard/32/318_2.png) [@milanleonard](https://discuss.pennylane.ai/u/milanleonard)\
**Post date:** [November 1, 2020, 9:02pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/16 "2020-11-01T21:02:16Z")

</div>

This is exactly what I need! Thanks so much Josh, I didn’t realise that stuff got shoved into the device object, will have to have a browse through the other methods on device!

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [November 2, 2020, 9:33am UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/17 "2020-11-02T09:33:25Z")

</div>

No worries @milanleonard! Note that this is an implementation detail of the Qiskit plugin, rather than a public API, so other plugins may make their internal components public using different names.

---

<div class="post-metadata">

**Author:** ![sassan\_moradi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sassan_moradi/32/422_2.png) [@sassan\_moradi](https://discuss.pennylane.ai/u/sassan_moradi)\
**Post date:** [January 31, 2021, 3:36pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/18 "2021-01-31T15:36:33Z")

</div>

Hi. I designed a quantum circuit with n\_qubits=n\_feature + 1(ancilla qubit). My goal is to do measurement on ancilla qubit not all qubits. How can i remove measurement gate on other qubits.

 ![quantum%20circuit](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/a4e38096c87d64a9b1cb17e21951a30905ed8e82.png)

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [February 1, 2021, 2:59pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/19 "2021-02-01T14:59:53Z")

</div>

Hi @sassan_moradi,

Thank you so much for your question! 🙂

You could do that in PennyLane by only specifying the single ancilla qubit in the return statement.

Feel free to also share code snippets and we could have a further look into your approach. Also do not hesitate to start a new thread dedicated to your question.

---

<div class="post-metadata">

**Author:** ![sassan\_moradi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sassan_moradi/32/422_2.png) [@sassan\_moradi](https://discuss.pennylane.ai/u/sassan_moradi)\
**Post date:** [February 2, 2021, 8:44am UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/20 "2021-02-02T08:44:25Z")

</div>

Here is the code:

 ![swap](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/466eb42531455054a0706bc3f611283b6d597cda.png)

import numpy as np  
import pennylane as qml  
import sklearn.decomposition  
import matplotlib

data1 = np.loadtxt(“PC-LH-fold0x.txt”)

data2 = np.loadtxt(“PC-LH-fold0y.txt”)

data3 = np.loadtxt(“PC-LH-fold0x-test.txt”)

data4 = np.loadtxt(“PC-LH-fold0y-test.txt”)

y\_train= data2[:,]

X\_train=data1[:]

y\_test= data4[:,]

X\_test=data3[:]

X\_train = sklearn.preprocessing.normalize(X\_train, norm=‘l2’,axis=1)

X\_test = sklearn.preprocessing.normalize(X\_test, norm=‘l2’, axis=1)

n\_features = 4  
n\_qubits = 2 \* n\_features + 1

dev = qml.device(“qiskit.aer”, wires=n\_qubits, shots=1024)

@qml.qnode(dev)  
def circuit(x1, x2):

```
qml.templates.MottonenStatePreparation(x1, wires=[1, 2, 3, 4])
qml.templates.MottonenStatePreparation(x2, wires=[5, 6, 7, 8])

qml.Hadamard(wires=0)
qml.CSWAP(wires=[0, 1, 4])
qml.CSWAP(wires=[0, 2, 5])
qml.CSWAP(wires=[0, 3, 6])
qml.CSWAP(wires=[0, 4, 7])
qml.Hadamard(wires=0)

return qml.expval(qml.PauliZ(wires=0))

```

x1 = list(map(abs, X\_train))  
x2 = list(map(abs, X\_test))  
x1 = x1[0]

x2 = x2[0]

circuit(x1, x2)

dev.\_circuit.draw(output=‘mpl’)

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [February 2, 2021, 4:42pm UTC](https://discuss.pennylane.ai/t/any-way-to-draw-my-quantum-circuit/290/21 "2021-02-02T16:42:45Z")

</div>

Hi @sassan_moradi,

Thank you so much!

Indeed, the underlying Qiskit `QuantumCircuit` will contain measurement operations on all qubits, this is what is shown on the circuit that is drawn. Having measurements on every qubit is simply a convenience that is applied when using the PennyLane-Qiskit plugin.

However, PennyLane will only consider the measurement outcomes on the `0th` wire, as specified.

One way to verify this is to use the built-in PennyLane circuit drawer after evaluating the QNode:

```python
circuit(x1, x2)
print(circuit.draw())

```

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/1278313335e9a870a387fdc251dca580fdf8937c.png)
