4
$\begingroup$

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[0], q[1], q[2]])
circuit.h(q[0])

circuit.draw()

Thanks in advance!

$\endgroup$
1
  • $\begingroup$ BTW, the line gate.label = gate_label is not necessary. Editing $\endgroup$
    – luciano
    Commented Apr 9, 2021 at 5:21

1 Answer 1

5
$\begingroup$

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[0], q[1], q[2]])
circuit.append(cnotnot('CNOTNOT_PRIME'), [q[0], q[1], q[2]])
circuit.h(q[0])

circuit.draw('mpl',
             style={'displaycolor': {'CNOTNOT': ('#000000', '#FFFFFF'),
                                           'h': ('#A1A1A1', '#043812')},
                    'displaytext': {'CNOTNOT_PRIME': "$\\mathrm{CNOTNOT}'$"}})

enter image description here

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – user15391
    Commented Apr 19, 2021 at 16:56
  • $\begingroup$ 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. $\endgroup$
    – luciano
    Commented Apr 19, 2021 at 18:31
  • $\begingroup$ 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? $\endgroup$
    – user15391
    Commented Apr 20, 2021 at 6:37
  • 1
    $\begingroup$ 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__. $\endgroup$
    – luciano
    Commented Apr 20, 2021 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.