Any way to draw my quantum circuit?

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.

Hi @Leeseok_Kim! You can use the 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 :slightly_smiling_face:

2 Likes

For example:

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

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

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

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.

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 :slight_smile:

In addition, you can also join our Slack channel: the link is 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.

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.

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

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:

>>> 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!

2 Likes

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

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.

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 text-based drawer.

Thanks!

1 Like

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

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().

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.

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:

>>> 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 
3 Likes

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!

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.

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.

Hi @sassan_moradi,

Thank you so much for your question! :slightly_smiling_face:

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.

Here is the code:

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’)

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:

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