Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.26k stars 2.37k forks source link

AttributeError when drawing circuit using Matplotlib #4439

Closed chowington closed 4 years ago

chowington commented 4 years ago

Information

What is the current behavior?

When trying to draw a QuantumCircuit object using .draw(output='mpl'), I'm getting AttributeError: 'Instruction' object has no attribute 'label' in qiskit/visualization/matplotlib.py. The circuit draws fine using the default text drawer.

Steps to reproduce the problem

The code is adapted from the linear_systems_of_equations HHL tutorial notebook. Here's a (probably non-minimal) snippet to reproduce:

from qiskit.aqua.algorithms import HHL
from qiskit.aqua.components.eigs import EigsQPE
from qiskit.circuit.library import QFT
from qiskit.aqua.components.reciprocals import LookupRotation
from qiskit.aqua.operators import MatrixOperator
from qiskit.aqua.components.initial_states import Custom

def create_eigs(matrix, num_ancillae, negative_evals):
    ne_qfts = [None, None]
    if negative_evals:
        num_ancillae += 1
        ne_qfts = [QFT(num_ancillae - 1), QFT(num_ancillae - 1, inverse=True)]

    return EigsQPE(MatrixOperator(matrix=matrix),
                   QFT(num_ancillae, inverse=True),
                   num_time_slices=50,
                   num_ancillae=num_ancillae,
                   expansion_mode='suzuki',
                   expansion_order=2,
                   evo_time=None,
                   negative_evals=negative_evals,
                   ne_qfts=ne_qfts)

matrix = [[1, 0], [0, 2]]
vector = [1, 4]

orig_size = len(vector)
matrix, vector, truncate_powerdim, truncate_hermitian = HHL.matrix_resize(matrix, vector)

# Initialize eigenvalue finding module
eigs = create_eigs(matrix, 3, False)
num_q, num_a = eigs.get_register_sizes()

# Initialize initial state module
init_state = Custom(num_q, state_vector=vector)

# Initialize reciprocal rotation module
reci = LookupRotation(negative_evals=eigs._negative_evals, evo_time=eigs._evo_time)

algo = HHL(matrix, vector, truncate_powerdim, truncate_hermitian, eigs,
           init_state, reci, num_q, num_a, orig_size)

circuit = algo.construct_circuit(measurement=True)
circuit.draw(output='mpl')

What is the expected behavior?

No errors, and the circuit is drawn correctly using Matplotlib.

Suggested solutions

Not sure - I don't know whether this could be a Terra visualization problem or an Aqua circuit design problem (or just a problem with the code above).

mtreinish commented 4 years ago

There is already a fix in progress for this issue: https://github.com/Qiskit/qiskit-terra/pull/4389 It's caused by certain gates (or circuit library elements that were converted to gates or instructions) missing a label attribute which the mpl drawer incorrectly assumes is always present.

aditya-giri commented 4 years ago

Hi, is there an ETA on a fix for this?

Cryoris commented 4 years ago

This should be part of the 0.15 release which is scheduled for two weeks. #4389 is fixing this 🙂

aditya-giri commented 4 years ago

Awesome, thanks 🎉