qutip / qutip-qip

The QuTiP quantum information processing package
https://qutip-qip.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
116 stars 63 forks source link

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

Open wangpeng1111 opened 1 week ago

wangpeng1111 commented 1 week ago

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 commented 1 day ago

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