CQCL / tket

Source code for the TKET quantum compiler, Python bindings and utilities
https://tket.quantinuum.com/
Apache License 2.0
243 stars 48 forks source link

ImportError: cannot import name '_TEMP_BIT_NAME' from 'pytket._tket.circuit' #1023

Closed fuglede closed 10 months ago

fuglede commented 10 months ago

When installing pytket in a fresh environment, using Python 3.11/Pip from Conda, then Pip to resolve all dependencies, I get the below error when importing anything the Qiskit-TKET converters. Versions are:

pip                       23.2.1          py311h06a4308_0  
python                    3.11.5               h955ad1f_0  
pytket                    1.19.1                   pypi_0    pypi
pytket-qiskit             0.39.0                   pypi_0    pypi
qiskit                    0.42.1                   pypi_0    pypi
qiskit-aer                0.12.0                   pypi_0    pypi
qiskit-dynamics           0.4.2                    pypi_0    pypi
qiskit-experiments        0.5.3                    pypi_0    pypi
qiskit-finance            0.3.4                    pypi_0    pypi
qiskit-ibm-experiment     0.3.5                    pypi_0    pypi
qiskit-ibm-runtime        0.9.3                    pypi_0    pypi
qiskit-ibmq-provider      0.20.2                   pypi_0    pypi
qiskit-machine-learning   0.6.1                    pypi_0    pypi
qiskit-nature             0.6.0                    pypi_0    pypi
qiskit-optimization       0.5.0                    pypi_0    pypi
qiskit-terra              0.23.3                   pypi_0    pypi

The full traceback is below:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[1], line 4
      2 import matplotlib.pyplot as plt
      3 from pytket.passes import RemoveRedundancies, CliffordSimp
----> 4 from pytket.extensions.qiskit import qiskit_to_tk, tk_to_qiskit

File ~/miniconda3/envs/synthesis/lib/python3.11/site-packages/pytket/extensions/qiskit/__init__.py:18
     16 # _metadata.py is copied to the folder after installation.
     17 from ._metadata import __extension_version__, __extension_name__  # type: ignore
---> 18 from .backends import (
     19     IBMQBackend,
     20     NoIBMQAccountError,
     21     AerBackend,
     22     AerStateBackend,
     23     AerUnitaryBackend,
     24     IBMQEmulatorBackend,
     25 )
     26 from .backends.config import set_ibmq_config
     27 from .qiskit_convert import qiskit_to_tk, tk_to_qiskit, process_characterisation

File ~/miniconda3/envs/synthesis/lib/python3.11/site-packages/pytket/extensions/qiskit/backends/__init__.py:16
      1 # Copyright 2019-2023 Cambridge Quantum Computing
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
   (...)
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 """Backends for connecting to IBM devices and simulators directly from pytket"""
---> 16 from .ibm import IBMQBackend, NoIBMQAccountError
     17 from .aer import AerBackend, AerStateBackend, AerUnitaryBackend
     18 from .ibmq_emulator import IBMQEmulatorBackend

File ~/miniconda3/envs/synthesis/lib/python3.11/site-packages/pytket/extensions/qiskit/backends/ibm.py:57
     55 from pytket.backends.backendresult import BackendResult
     56 from pytket.backends.resulthandle import _ResultIdTuple
---> 57 from pytket.extensions.qiskit.qiskit_convert import (
     58     process_characterisation,
     59     get_avg_characterisation,
     60 )
     61 from pytket.extensions.qiskit._metadata import __extension_version__
     62 from pytket.passes import (  # type: ignore
     63     BasePass,
     64     auto_rebase_pass,
   (...)
     74     NaivePlacementPass,
     75 )

File ~/miniconda3/envs/synthesis/lib/python3.11/site-packages/pytket/extensions/qiskit/qiskit_convert.py:72
     58 from qiskit.extensions.unitary import UnitaryGate  # type: ignore
     59 from pytket.circuit import (  # type: ignore
     60     CircBox,
     61     Circuit,
   (...)
     70     QControlBox,
     71 )
---> 72 from pytket._tket.circuit import _TEMP_BIT_NAME  # type: ignore
     73 from pytket.pauli import Pauli, QubitPauliString  # type: ignore
     74 from pytket.architecture import Architecture, FullyConnected  # type: ignore

ImportError: cannot import name '_TEMP_BIT_NAME' from 'pytket._tket.circuit' (/home/user/miniconda3/envs/synthesis/lib/python3.11/site-packages/pytket/_tket/circuit.cpython-311-x86_64-linux-gnu.so)
fuglede commented 10 months ago

As a workaround, I can remove the broken import on qiskit_convert.py:72, and change qiskit_convert.py:637 from

        if b.reg_name != _TEMP_BIT_NAME and (

to

        if b.reg_name != 60 and (

and get something that seems to work (without knowing what _TEMP_BIT_NAME was supposed to be).

cqc-alec commented 10 months ago

Yes, the latest update to pytket (1.19) requires that pytket-qiskit also be updated. Please could you update to the latest pytket-qiskit and try again? Thanks!

cqc-alec commented 10 months ago

Closing this: please reopen if still an issue after updating pytket.

fuglede commented 10 months ago

The versions above were obtained by installing the packages in a blank environment (alongside a handful of other packages), so Pip's solver found the versions to be compatible; maybe some constraints are missing in the dependency specification of pytket then?

Moreover, if I run pip install --upgrade pytket-qiskit, it mages to go from 0.39.0 to 0.44.0, but the exact same error occurs.

fuglede commented 10 months ago

Looks like I can't reopen the issue.

cqc-alec commented 10 months ago

You probably have to pip install --upgrade pytket as well.

This was in fact a bug, in that we updated pytket without realizing that doing so would break pytket-qiskit: we normally try to keep backwards compatibility in pytket 1.x releases, but forgot that some of the extensions import underscored symbols which aren't covered by that guarantee.

fuglede commented 10 months ago

That takes me from pytket-1.19.1 to pytket-1.20.1; the error persists.

fuglede commented 10 months ago

Edit; sorry, what's failing is a manual run of

from pytket._tket.circuit import _TEMP_BIT_NAME

but I noticed qiskit_convert.py effectively branches on version numbers now, avoiding this import, so this isn't an issue in practice.

cqc-alec commented 10 months ago

OK, sorry for the inconvenience this has caused.

fuglede commented 10 months ago

No worries, thanks for your work on TKET!

tanmay0550 commented 10 months ago

I used the above suggestion, but it's still showing the same issue

cannot import name '_TEMP_BIT_NAME' from 'pytket._tket.circuit

cqc-alec commented 10 months ago

I used the above suggestion, but it's still showing the same issue

cannot import name '_TEMP_BIT_NAME' from 'pytket._tket.circuit

Is this using pytket-qiskit? Which version?