Qiskit / qiskit-metapackage

Qiskit is an open-source SDK for working with quantum computers at the level of circuits, algorithms, and application modules.
https://qiskit.org
Apache License 2.0
3.03k stars 751 forks source link

`classical_function` decorator raises import error missing `tweedledum` #1253

Closed idnm closed 3 years ago

idnm commented 3 years ago

Informations

What is the current behavior?

@classical_function decorator raises error MissingOptionalLibraryError: "The 'tweedledum' library is required to use 'classical function compiler'. You can install it with 'pip install tweedledum'."

Steps to reproduce the problem

In IBM quantum lab

import numpy as np
# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, transpile, Aer, IBMQ
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from ibm_quantum_widgets import *

# Loading your IBM Q account(s)
provider = IBMQ.load_account()

from qiskit.circuit import classical_function,  Int1

@classical_function
def grover_oracle(a: Int1, b: Int1, c: Int1, d: Int1) -> Int1:
    return (not a and b and not c and d)

quantum_circuit = grover_oracle.synth()
quantum_circuit.draw()

The first block is the standard import, the second block is copied from docs

What is the expected behavior?

No import error and and output as in the docs.

Suggested solutions

Comments

I get the same error on my system where tweedledum is installed. I thought my installation might be messed up but the same problem shows up in the cloud.

Paul-Hermes commented 3 years ago

I think the problem lies in qiskit-terra/qiskit/circuit/classicalfunction/utils.py where we have this

try:
    from tweedledum.ir import Qubit  # pylint: disable=no-name-in-module
    from tweedledum.passes import parity_decomp  # pylint: disable=no-name-in-module

    HAS_TWEEDLEDUM = True
except Exception:  # pylint: disable=broad-except
    HAS_TWEEDLEDUM = False

I'm guessing it's some other sort of error happening during the import so we should change the Exception to ImportError. Haven't had a chance to test this yet though.

rafal-pracht commented 3 years ago

This solution only hides the issue not resolves it. I've reproduced the issue locally and it was solved by installing the missing library 'pip install tweedledum'. I think is better to add this library to dependencies.