# How to plot custom gate labels in Qiskit?

How can I create a custom gate in qiskit that is plotted using latex formatting (or another type of formatting)? For example the following code generates a circuit diagram

from qiskit import QuantumCircuit
from qiskit.circuit import Gate

qc = QuantumCircuit(1, 1)
qc.append(Gate(name=r'X_t', num_qubits=1, params=[]), [0])
qc.measure([0, ], [0,])
qc.draw(output='latex', interactive=False, filename='test.png')


The custom gate is labelled as X_t, but I want the name to be formatted as an X with subscript t. I tried variations as

qc.append(Gate(name=r'$$X_t$$', num_qubits=1, params=[]), [0])


but that does not work.

Note: reported on github as well https://github.com/Qiskit/qiskit-terra/issues/3171

The problem seems to stem from the utf8tolatex parsing in the pylatexenc.latexencode package, line 370 here. Use of this function is not yet depreciated, but it is discouraged, and it has the unfortunate behavior of escaping the underscore, changing 'X_t' to 'X{\\_}t' (this is already inserted into the latex source in a math environment, so no need for $...$). A flag to disable this parsing and pass the string directly to \\gate{%s} would be very nice.

Also the mpl plots have a style dict kwarg structure, so that you can arbitrarily change the default name mapping with a JSON style sheet. Unfortunately, the latex plots ignore these style sheets and the mapping is hardcoded, e.g. line 384.

As far as I can tell, right now there's no way to accomplish what you want without running off your own branch. That is, unless anybody knows a trick to get utf8tolatex to stop escaping the underscore.

There is not a way to have the latex drawer do the subscript currently. As ChainedSymmetry pointed out the use of pylatexenc prevents that because it will escape or convert the underscore and symbols to their latex equivalents. This was added because when we first added custom gates support to the drawer people had issues with passing things like custom_gate to the drawer it would make the 'g' a subscript (see https://github.com/Qiskit/qiskit-terra/pull/2246 ). While I still think this is a sane default for people, I agree we probably should add a flag to disable the conversion to the draw() functions, so I quickly wrote up a patch for that here:

https://github.com/Qiskit/qiskit-terra/pull/3172

That being said the mpl drawer should work fine in the meantime for your second example with the label name=r'$X_t$'. That should tell matplotlib to use math mode when rendering the gates name and use a subscript.

I don't think that this is currently possible with Qiskit, could you please add this as an Enhancement to the GitHub repo? This is done by creating an Issue and then selecting that it is an Enhancement.