Circuit visualization

Hi,
I tried to use Qiskit to visualize the circuit, but it was unsuccessful.(Pennylane=0.33.1)

import pennylane as qml
dev = qml.device("qiskit.aer", wires=4)

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

params = [0.1, 0.3]
drawer = qml.draw(circuit, show_all_wires=True, wire_order=[2,1,0,3])
# print(drawer(params))
circuit(params)
dev._circuit.draw(output="mpl")

Error:

----> 34 circuit(params)
      35 dev._circuit.draw(output="mpl")

File d:\Software\Anaconda3\anaconda3\envs\pennylane\lib\site-packages\pennylane\qnode.py:1027, in QNode.__call__(self, *args, **kwargs)
   1022         full_transform_program._set_all_argnums(
   1023             self, args, kwargs, argnums
   1024         )  # pylint: disable=protected-access
   1026 # pylint: disable=unexpected-keyword-arg
-> 1027 res = qml.execute(
   1028     (self._tape,),
   1029     device=self.device,
   1030     gradient_fn=self.gradient_fn,
   1031     interface=self.interface,
   1032     transform_program=full_transform_program,
   1033     config=config,
   1034     gradient_kwargs=self.gradient_kwargs,
   1035     override_shots=override_shots,
   1036     **self.execute_kwargs,
   1037 )
   1039 res = res[0]
   1041 # convert result to the interface in case the qfunc has no parameters
...
--> 502 if not pennylane.active_return():
    503     res = self._statistics_legacy(circuit)
    504     res = np.asarray(res)

AttributeError: module 'pennylane' has no attribute 'active_return'

How can I fix it to avoid mistakes?
I would appreciate it if you could give me some guidance.

Hello @SHAN, this code runs your circuit inside Qiskit Lab or Colab:

!pip install pennylane-qiskit # run once
import pennylane as qml
dev = qml.device("qiskit.aer", wires=4)

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

params = [0.1, 0.3]
print(qml.draw_mpl(circuit, wire_order=[2,1,0,3], show_all_wires=True, decimals=1)(params))

Help Colleague 11-11-23

Hello @kevinkawchak. First of all, thank you very much for your help.
In fact, I want to know how to use the Qiskit method to visualize the circuit.(Use:dev._circuit.draw(output=“mpl”))

Hello @SHAN !

I want to know how to use the Qiskit method to visualize the circuit.

If you don’t mind the question, is there any particular reason to use Qiskit method?

In the previous versions of Pennylane, as you can see in this blog post from 2021, it was possible to draw the circuit as you pointed out.

However, a lot of things changed since then! Pennylane evolved and we released a very nice circuit drawer. You can find more details about usage here.

I hope it helps! :slight_smile:

1 Like

Hello @ludmilaaasb ,I just wanted to learn about this method for no particular reason. Anyway thanks for your help! :grin:

1 Like