Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom gates in LaTeX drawings do not display names, just a "U" #252

Open
wangpeng1111 opened this issue Sep 26, 2024 · 1 comment
Open

Comments

@wangpeng1111
Copy link

hello, I have a question in https://qutip-qip.readthedocs.io/en/latest/qip-simulator.html.

from qutip_qip.circuit import QubitCircuit
from qutip_qip.operations import (
Gate, controlled_gate, hadamard_transform)
def controlled_hadamard():
# Controlled Hadamard
return controlled_gate(
hadamard_transform(1), controls=0, targets=1, control_value=1)
qc = QubitCircuit(N=3, num_cbits=3)
qc.user_gates = {"cH": controlled_hadamard}
qc.add_gate("QASMU", targets=[0], arg_value=[1.91063, 0, 0])
qc.add_gate("cH", targets=[0,1])
qc.add_gate("TOFFOLI", targets=[2], controls=[0, 1])
qc.add_gate("X", targets=[0])
qc.add_gate("X", targets=[1])
qc.add_gate("CNOT", targets=[1], controls=0)

in the circuit , the custom gate "cH" can show in here, but when I try to custom another gate, there are just a "U" in the circuit, it cannot display defined names. when I copy this code in a new jupyter notebook, it also show "U" in the circuit, and not "cH" like https://qutip-qip.readthedocs.io/en/latest/qip-simulator.html.

I want to ask, how can I display a custom name in circuit with latex.

@BoxiLi
Copy link
Member

BoxiLi commented Oct 3, 2024

The default latex_str is set to U. There are several ways to redefine this.

  1. In gateclass.py, change self.latex_str = r"U" to self.latex_str = self.name. This sets the default latex string to be the gate name.
  2. Hack a bit and directly modify the stored gate object. Add qc.gates[1].latex_str="cH". This is not recommended as the behaviour may change in the future, but this is a very quick hack fix for just this single gate.

BTW, if you are willing to install the master branch, we have recently added a new plot mechanism and where you will get beautiful matplotlib plots by qc.draw("matplotlib")
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants