unitaryfund / mitiq

Mitiq is an open source toolkit for implementing error mitigation techniques on most current intermediate-scale quantum computers.
https://mitiq.readthedocs.io
GNU General Public License v3.0
363 stars 160 forks source link

Not all folding methods support QPROGRAM #873

Closed crazy4pi314 closed 3 years ago

crazy4pi314 commented 3 years ago

Pre-Report Checklist

Issue Description

It seems like many of the helper functions for folding only work for Circ circuits, but seem to pass some tests when given Qiskit circuits with seemingly no conversion. See https://github.com/unitaryfund/mitiq/blob/master/mitiq/zne/scaling/folding.py#L509 for example, which says it take only Circ circuit types but seems to work with the Qiskit ones. I know type hints are not binding, but we can see that one of the functions called by fold from right/left fails when given a Qiskit circuit.

How to Reproduce

image

Code Snippet

Sample from image

from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister
from mitiq.zne.scaling.folding import fold_gates_from_right, _create_weight_mask

qiskit_qreg = QuantumRegister(3)
qiskit_creg = ClassicalRegister(3)
qiskit_circuit = QuantumCircuit(qiskit_qreg, qiskit_creg)
qiskit_circuit.h(qiskit_qreg)
qiskit_circuit.cnot(qiskit_qreg[0], qiskit_qreg[1])
qiskit_circuit.t(qiskit_qreg[2])
qiskit_circuit.ccx(*qiskit_qreg)
qiskit_circuit.measure(qiskit_qreg, qiskit_creg)

print(qiskit_circuit)
print(type(qiskit_circuit))

qiskit_folded_circuit = fold_gates_from_right(
    qiskit_circuit, scale_factor=1.0)
print(qiskit_folded_circuit)
print(type(qiskit_folded_circuit))

_create_weight_mask(qiskit_circuit, {"H": 0.9, "CNOT": 0.8, "T": 0.7, "TOFFOLI": 0.6})

Error Output

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-3c3e6852ced4> in <module>
      7 qiskit_circuit.ccx(*qiskit_qreg)
      8 qiskit_circuit.measure(qiskit_qreg, qiskit_creg)
----> 9 _create_weight_mask(qiskit_circuit, {"H": 0.9, "CNOT": 0.8, "T": 0.7, "TOFFOLI": 0.6})

~\git\unitaryfund\mitiq\mitiq\zne\scaling\folding.py in _create_weight_mask(circuit, fidelities)
    332     return [
    333         _get_weight_for_gate(weights, op)
--> 334         for op in circuit.all_operations()
    335         if not _is_measurement(op)
    336     ]

AttributeError: 'QuantumCircuit' object has no attribute 'all_operations'

Environment Context

Use the about() function to summarize information on operating system, python version and dependencies.

Mitiq: A Python toolkit for implementing error mitigation on quantum computers
==============================================================================
Authored by: Mitiq team, 2020 & later (https://github.com/unitaryfund/mitiq)

Mitiq Version:  0.10.0dev

Core Dependencies
-----------------
Cirq Version:   0.10.0
NumPy Version:  1.20.3
SciPy Version:  1.4.1

Optional Dependencies
---------------------
PyQuil Version: 2.28.2
Qiskit Version: None
Braket Version: 1.5.16

Python Version: 3.7.6
Platform Info:  Windows (AMD64)

Additional Python Environment Details (pip freeze or conda list):

# packages in environment at C:\tools\Anaconda3\envs\mitiq-test:       
#
# Name                    Version                   Build  Channel     
alabaster                 0.7.12                   pypi_0    pypi      
amazon-braket-default-simulator 1.3.0                    pypi_0    pypi
amazon-braket-schemas     1.3.0                    pypi_0    pypi      
amazon-braket-sdk         1.5.16                   pypi_0    pypi      
anyio                     3.3.0                    pypi_0    pypi      
appdirs                   1.4.4                    pypi_0    pypi      
atomicwrites              1.4.0                    pypi_0    pypi      
attrs                     20.3.0                   pypi_0    pypi      
babel                     2.9.1                    pypi_0    pypi      
backoff                   1.11.1                   pypi_0    pypi      
beautifulsoup4            4.9.3                    pypi_0    pypi      
black                     19.10b0                  pypi_0    pypi      
bleach                    4.1.0                    pypi_0    pypi      
boltons                   21.0.0                   pypi_0    pypi      
boto3                     1.18.29                  pypi_0    pypi      
botocore                  1.21.29                  pypi_0    pypi      
ca-certificates           2021.7.5             haa95532_1
cachetools                4.2.2                    pypi_0    pypi      
certifi                   2021.5.30        py39haa95532_0
charset-normalizer        2.0.4                    pypi_0    pypi
cirq                      0.10.0                   pypi_0    pypi
click                     8.0.1                    pypi_0    pypi
coverage                  5.5                      pypi_0    pypi
cryptography              3.4.8                    pypi_0    pypi
cycler                    0.10.0                   pypi_0    pypi
dill                      0.3.4                    pypi_0    pypi
docutils                  0.16                     pypi_0    pypi
entrypoints               0.3                      pypi_0    pypi
execnet                   1.9.0                    pypi_0    pypi
fastjsonschema            2.15.1                   pypi_0    pypi
flake8                    3.7.9                    pypi_0    pypi
gitdb                     4.0.7                    pypi_0    pypi
gitpython                 3.1.18                   pypi_0    pypi
google-api-core           1.31.2                   pypi_0    pypi
google-auth               1.35.0                   pypi_0    pypi
googleapis-common-protos  1.53.0                   pypi_0    pypi
greenlet                  1.1.1                    pypi_0    pypi
grpcio                    1.39.0                   pypi_0    pypi
idna                      3.2                      pypi_0    pypi
imagesize                 1.2.0                    pypi_0    pypi
immutables                0.6                      pypi_0    pypi
importlib-metadata        4.6.4                    pypi_0    pypi
iniconfig                 1.1.1                    pypi_0    pypi
jmespath                  0.10.0                   pypi_0    pypi
jupyter-cache             0.4.3                    pypi_0    pypi
jupyter-client            7.0.1                    pypi_0    pypi
jupyter-server            1.10.2                   pypi_0    pypi
jupyter-server-mathjax    0.2.3                    pypi_0    pypi
jupyter-sphinx            0.3.2                    pypi_0    pypi
kiwisolver                1.3.1                    pypi_0    pypi
lark                      0.11.3                   pypi_0    pypi
latexcodec                2.0.1                    pypi_0    pypi
markdown-it-py            0.6.2                    pypi_0    pypi
matplotlib                3.4.3                    pypi_0    pypi
mccabe                    0.6.1                    pypi_0    pypi
mdit-py-plugins           0.2.6                    pypi_0    pypi
mitiq                     0.10.0.dev0               dev_0    <develop>
mpmath                    1.2.1                    pypi_0    pypi
msgpack                   0.6.2                    pypi_0    pypi
mypy                      0.910                    pypi_0    pypi
mypy-extensions           0.4.3                    pypi_0    pypi
myst-nb                   0.12.3                   pypi_0    pypi
myst-parser               0.13.7                   pypi_0    pypi
nbclient                  0.5.4                    pypi_0    pypi
nbconvert                 5.6.1                    pypi_0    pypi
nbdime                    3.1.0                    pypi_0    pypi
networkx                  2.6.2                    pypi_0    pypi
ntlm-auth                 1.5.0                    pypi_0    pypi
numpy                     1.20.1                   pypi_0    pypi
openssl                   1.1.1k               h2bbff1b_0
opt-einsum                3.3.0                    pypi_0    pypi
pandas                    1.3.2                    pypi_0    pypi
pathspec                  0.9.0                    pypi_0    pypi
pillow                    8.3.1                    pypi_0    pypi
pip                       21.2.4           py38haa95532_0
pluggy                    0.13.1                   pypi_0    pypi
ply                       3.11                     pypi_0    pypi
prompt-toolkit            3.0.20                   pypi_0    pypi
protobuf                  3.13.0                   pypi_0    pypi
psutil                    5.8.0                    pypi_0    pypi
py                        1.10.0                   pypi_0    pypi
pyasn1                    0.4.8                    pypi_0    pypi
pyasn1-modules            0.2.8                    pypi_0    pypi
pybind11                  2.7.1                    pypi_0    pypi
pybtex                    0.24.0                   pypi_0    pypi
pybtex-docutils           1.0.1                    pypi_0    pypi
pycodestyle               2.5.0                    pypi_0    pypi
pydantic                  1.8.2                    pypi_0    pypi
pydata-sphinx-theme       0.6.3                    pypi_0    pypi
pyflakes                  2.1.1                    pypi_0    pypi
pyquil                    2.28.2                   pypi_0    pypi
pytest                    6.2.4                    pypi_0    pypi
pytest-cov                2.11.1                   pypi_0    pypi
pytest-forked             1.3.0                    pypi_0    pypi
pytest-xdist              2.2.1                    pypi_0    pypi
python                    3.9.6                h6244533_1
python-constraint         1.4.0                    pypi_0    pypi
python-rapidjson          1.4                      pypi_0    pypi
pytz                      2021.1                   pypi_0    pypi
pywin32                   301                      pypi_0    pypi
pywinpty                  1.1.3                    pypi_0    pypi
pyyaml                    5.4.1                    pypi_0    pypi
qiskit-aer                0.8.2                    pypi_0    pypi
qiskit-ibmq-provider      0.16.0                   pypi_0    pypi
qiskit-terra              0.18.1                   pypi_0    pypi
qtconsole                 5.1.1                    pypi_0    pypi
regex                     2021.8.21                pypi_0    pypi
requests                  2.26.0                   pypi_0    pypi
requests-ntlm             1.1.0                    pypi_0    pypi
requests-unixsocket       0.2.0                    pypi_0    pypi
retworkx                  0.10.0                   pypi_0    pypi
rpcq                      3.9.2                    pypi_0    pypi
rsa                       4.7.2                    pypi_0    pypi
ruamel-yaml               0.17.14                  pypi_0    pypi
ruamel-yaml-clib          0.2.6                    pypi_0    pypi
s3transfer                0.5.0                    pypi_0    pypi
scipy                     1.7.1                    pypi_0    pypi
send2trash                1.8.0                    pypi_0    pypi
setuptools                52.0.0           py39haa95532_0
smmap                     4.0.0                    pypi_0    pypi
sniffio                   1.2.0                    pypi_0    pypi
snowballstemmer           2.1.0                    pypi_0    pypi
sortedcontainers          2.4.0                    pypi_0    pypi
soupsieve                 2.2.1                    pypi_0    pypi
sphinx                    3.5.4                    pypi_0    pypi
sphinx-autodoc-typehints  1.12.0                   pypi_0    pypi
sphinx-copybutton         0.3.3                    pypi_0    pypi
sphinx-togglebutton       0.2.3                    pypi_0    pypi
sphinxcontrib-applehelp   1.0.2                    pypi_0    pypi
sphinxcontrib-bibtex      2.2.1                    pypi_0    pypi
sphinxcontrib-devhelp     1.0.2                    pypi_0    pypi
sphinxcontrib-htmlhelp    2.0.0                    pypi_0    pypi
sphinxcontrib-jsmath      1.0.1                    pypi_0    pypi
sphinxcontrib-qthelp      1.0.3                    pypi_0    pypi
sphinxcontrib-serializinghtml 1.1.5                    pypi_0    pypi
sqlalchemy                1.4.23                   pypi_0    pypi
sqlite                    3.36.0               h2bbff1b_0
sympy                     1.8                      pypi_0    pypi
terminado                 0.11.1                   pypi_0    pypi
toml                      0.10.2                   pypi_0    pypi
tqdm                      4.62.2                   pypi_0    pypi
tweedledum                1.1.0                    pypi_0    pypi
typed-ast                 1.4.3                    pypi_0    pypi
typing-extensions         3.10.0.0                 pypi_0    pypi
tzdata                    2021a                h5d7bf9c_0
urllib3                   1.26.6                   pypi_0    pypi
vc                        14.2                 h21ff451_1
vs2015_runtime            14.27.29016          h5e58377_2
webencodings              0.5.1                    pypi_0    pypi
websocket-client          1.2.1                    pypi_0    pypi
wheel                     0.37.0             pyhd3eb1b0_0
wincertstore              0.2              py39h2bbff1b_0
crazy4pi314 commented 3 years ago

@rmlarose is this correct? I also couldn't work out exactly how existing methods supported Qiskit circuits 😓

rmlarose commented 3 years ago

_create_weight_mask is a helper function specific to cirq.Circuits. The public folding functions https://github.com/unitaryfund/mitiq/blob/a3d6bd773798af3ec3e7a555ea6a1b91e207be1e/mitiq/zne/scaling/__init__.py#L17-L27 are for any QPROGRAM. I think mypy complains if these public functions are not annotated with cirq.Circuits, but anything with the @noise_scaling_converter decorator accepts any QPROGRAM.

rmlarose commented 3 years ago

I think this can be closed. Please reopen if appropriate.