# Is there any way to assign a colour for a single gate in Qiskit?

I'm drawing with Qiskit a big circuit with some predefined and custom gates. I want to colour some of them in non-default colours for a better reading of it. Is it possible to do it for predefined gates? And for custom ones? Here is some code to play with:

from qiskit import QuantumCircuit, QuantumRegister

def cnotnot(gate_label = 'CNOTNOT'):

gate_circuit = QuantumCircuit(3, name = gate_label)
gate_circuit.cnot(0, 1)
gate_circuit.cnot(0, 2)

gate = gate_circuit.to_gate()

return gate

q = QuantumRegister(3, name = 'q')

circuit = QuantumCircuit(q)

circuit.append(cnotnot(), [q, q, q])
circuit.h(q)

circuit.draw()


Thanks in advance!

• BTW, the line gate.label = gate_label is not necessary. Editing – luciano Apr 9 at 5:21

## 1 Answer

I think it is something Qiskit used to have but got lost in a refactoring. I'm adding it back here https://github.com/Qiskit/qiskit-terra/pull/6184 (with a regression test so it does not happen again) and it will probably be released in the next qiskit-terra patch version (0.17.1).

Here is your example (including displaytext):

from qiskit import QuantumCircuit, QuantumRegister

def cnotnot(gate_label = 'CNOTNOT'):

gate_circuit = QuantumCircuit(3, name = gate_label)
gate_circuit.cnot(0, 1)
gate_circuit.cnot(0, 2)

gate = gate_circuit.to_gate()

return gate

q = QuantumRegister(3, name = 'q')

circuit = QuantumCircuit(q)

circuit.append(cnotnot(), [q, q, q])
circuit.append(cnotnot('CNOTNOT_PRIME'), [q, q, q])
circuit.h(q)

circuit.draw('mpl',
style={'displaycolor': {'CNOTNOT': ('#000000', '#FFFFFF'),
'h': ('#A1A1A1', '#043812')},
'displaytext': {'CNOTNOT_PRIME': "$$\\mathrm{CNOTNOT}'$$"}}) • Hi, thanks for your answer! Launching in Binder the following .ipynb it doesn't work for custom gates but it works for the Hadamard gate github.com/sebastianvromero/qecc_shor9q. I have separated notebooks for functions and the circuit itself, but it also doesn't work if I define and call the custom gate in the same notebook. – Sebastián V. Romero Apr 19 at 16:56
• It does work to me in your notebook. In your QECC_Shor9Q.ipynb, cell 3, change your last line for circuit.draw(output = 'mpl', plot_barriers = False, style={'displaycolor': {'CNOTNOT': ('#000000', '#FFFFFF')}}). Let me know. – luciano Apr 19 at 18:31
• it's a bit strange but that's what I wrote and now it works, thanks for your answer! Could it be related to the upgrade of versions in the different qiskit packages? – Sebastián V. Romero Apr 20 at 6:37
• indeed. probably the binder container was pulling from a cache. You can always check the qiskit version you are running with from qiskit import __qiskit_version__. – luciano Apr 20 at 9:07