TransWikia.com

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

Quantum Computing Asked by Sebastián V. Romero on May 21, 2021

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!

One 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[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

Correct answer by luciano on May 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP