Hello! I’m using the qml.draw_mpl() to draw diagrams of my quantum circuits, and I want to customize the texts written in the quantum gates in the diagrams. For example, the following code will generate a diagram with one quantum gate with text ‘RX’:
Now, I want to replace the text ‘RX’ with something else (for example, ‘The first rotation’). I find that the following code will generate a similar diagram with the text ‘RX(“The first rotation”)’:
If your purpose is simply to draw the circuit then you don’t even need a qnode.
In the docs for the MPLDrawer class you can see some examples of “box_gate” with custom labels. You can even make the gate larger than the others as shown in the last example there.
Let me know if this answers your question!
If you need the gate for more than just the drawing then you might need to create a custom gate. I can point you in the right direction if that’s the case.
Hi @CatalinaAlbornoz , thank you for pointing the simple way to draw circuits! However, I still encounter some problems when using the MPLDrawer. For illustration, the following code generates a strange diagram:
import pennylane as qml
from matplotlib import pyplot as plt
def draw_attention_module():
drawer=qml.drawer.MPLDrawer(2, {i: i for i in range(2)})
drawer.box_gate(layer=0, wires=0, text='Rotation 1')
drawer.ctrl(layer=1, wires=0, control_values=True)
drawer.box_gate(layer=1, wires=1, text='Rotation 2') # This 2 statements are to draw a controlled gate
qml.drawer.use_style('black_white') # It is intended to generate a black and white diagram, while it did not
draw_attention_module()
plt.show()
The color of the diagram is not black and white as the statement qml.drawer.use_style('black_white') requests, and there lacks a line between the controlling qubit and the controlled gate box in layer 1. Besides, I also want to know how to define which gate is the controlled gate if there is multiple gates in a same layer and just one of them is intended to be a controlled gate?
Thanks for bringing this up, it looks like there’s a bug in the use of styles. Fortunately there’s an easy workaround. Instead of using use_style you can call the style directly: qml.drawer.style._black_white(). You can use other styles too by just using style and adding an underscore before the name of the style.
Regarding the control, precisely your last question is the reason why you weren’t getting the “line” in the control before. The drawer cannot know what gate you’re controlling so the trick here is to add a control on two gates, and then hide one of the controls behing the box_gate. You can do this by using box_options={"zorder": 3}, which sets the box_gate at level 3 from back to front, where {"zorder": 0} would mean behind everything including the circuit line and the control. In this case level 3 is enough to keep this gate in front of everything else.
The code below should give you the circuit you need.
import pennylane as qml
from matplotlib import pyplot as plt
def draw_attention_module():
drawer=qml.drawer.MPLDrawer(2, {i: i for i in range(2)})
drawer.box_gate(layer=0, wires=0, text='Rotation 1')
drawer.ctrl(layer=1, wires=[0, 1], control_values=[True, True]) # Add a second control wire and a second control value
drawer.box_gate(layer=1, wires=1, text='Rotation 2', box_options={"zorder": 3}) # Add box_options={"zorder": 3}
#qml.drawer.use_style('black_white') # This doesn't work
qml.drawer.style._black_white() # This works
draw_attention_module()
plt.show()
Let us know if you have any further questions and thanks for helping us uncover the bug in the styles!
Hello! I have an additional question. Can I write texts on the right of the circuit to denote the output state when using the MPLDrawer? I find that I can use drawer.label() to put texts on the left to denote the input state, but I don’t know how to put texts on right.
You can take the figure and axes from the drawer and use them with Matplotlib methods such as annotate. Here’s an example:
import pennylane as qml
from matplotlib import pyplot as plt
def draw_attention_module():
drawer=qml.drawer.MPLDrawer(2, {i: i for i in range(2)})
drawer.box_gate(layer=0, wires=0, text='Rotation 1')
drawer.ctrl(layer=1, wires=[0, 1], control_values=[True, True]) # Add a second control wire and a second control value
drawer.box_gate(layer=1, wires=1, text='Rotation 2', box_options={"zorder": 3}) # Add box_options={"zorder": 3}
# use annotate
drawer.ax.annotate('annotate', xy=(0, 0), xytext=(2.1, 0))
qml.drawer.style._black_white()
draw_attention_module()
plt.show()