Qiskit-terra: MPL drawer support for complex custom instructions

Created on 5 Oct 2019  ยท  5Comments  ยท  Source: Qiskit/qiskit-terra

The following circuit is being draw like wrongly:

from qiskit.circuit import Gate, Parameter
qinst = QuantumRegister(1, 'q')
cinst = ClassicalRegister(1, 'c')

inst = QuantumCircuit(qinst, cinst, name='instruction').to_instruction()

qr = QuantumRegister(2, 'q')
cr = ClassicalRegister(2, 'c')
circuit = QuantumCircuit(qr, cr)
circuit.append(inst, [qr[0]], [cr[1]])
circuit.draw(output='mpl')

image

It should be similar to this:
Considering the following custom instruction:

        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”            
q_0: |0>โ”ค1            โ”œ            
        โ”‚             โ”‚            
q_1: |0>โ”ค             โ”œ            
        โ”‚ instruction โ”‚            
 c_0: 0 โ•ก             โ•ž            
        โ”‚             โ”‚            
 c_1: 0 โ•ก0            โ•ž            
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Additionally, based on the consensus in #3073, the mpl drawer should support controlled/conditional custom instructions. The following is an example for conditional:

from qiskit.circuit import Gate, Parameter
qinst = QuantumRegister(1, 'q')
cinst = ClassicalRegister(1, 'c')

inst = QuantumCircuit(qinst, cinst, name='instruction').to_instruction()

qr = QuantumRegister(2, 'q')
cr = ClassicalRegister(2, 'c')
circuit = QuantumCircuit(qr, cr)
circuit.append(inst, [qr[0]], [cr[1]]).c_if(cr, 2)
circuit.draw(output='mpl')

Controlled is not yet supported (see #2862).

bug low

Most helpful comment

hi,
i think i have the solution for that , can you assign it to me so that i can look into it.

All 5 comments

hi,
i think i have the solution for that , can you assign it to me so that i can look into it.

@yesodharan1892 can you draw instances of ControlledGate in matplotlib's circuit visualizer? This is kind of high priority. Currently this is only doable in the text drawer (https://github.com/Qiskit/qiskit-terra/pull/3442)

hi @ajavadia i have made some changes in the code given above and the mpl visualization is what as is expected, but when i look at the text visualization it seems to be wrong.

from qiskit import *
from qiskit.circuit import Gate, Parameter
qr = QuantumRegister(2, 'q')
cr = ClassicalRegister(2, 'c')
inst = QuantumCircuit(qr, cr, name='instruction').to_instruction()
%matplotlib inline
circuit = QuantumCircuit(qr, cr)
circuit.append(inst, [qr[1], qr[0]],[cr[0],cr[1]])
circuit.draw(output='mpl')

Untitled

Untitled
this is the expectated visualization for q0 acting on c0 as it is shown below.

Untitled

i look at the latex visualization it seems to be wrong.

you mean "text visualizer"?

yes i m sorry , i meant text visualizer...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ewinston picture ewinston  ยท  3Comments

jaygambetta picture jaygambetta  ยท  5Comments

cbjuan picture cbjuan  ยท  3Comments

indisoluble picture indisoluble  ยท  3Comments

ajavadia picture ajavadia  ยท  5Comments