Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with the extended_stabilizer method #1781

Closed
acastanedam opened this issue Apr 14, 2023 · 2 comments
Closed

Problems with the extended_stabilizer method #1781

acastanedam opened this issue Apr 14, 2023 · 2 comments
Assignees
Labels
bug Something isn't working duplicate This issue or pull request already exists
Milestone

Comments

@acastanedam
Copy link
Contributor

acastanedam commented Apr 14, 2023

Informations

  • Qiskit AER version: main (@0.13.0)
  • Operating system: Linux

What is the current behavior?

Aersimulator run and estimator do not work when method=extended_stabilizer

Steps to reproduce the problem

This small script shows what is happening:

import numpy as np
from qiskit_aer import AerSimulator
from qiskit.circuit.library import EfficientSU2
from qiskit_aer.primitives import Estimator as aer_estimator
from qiskit import transpile

backend = AerSimulator(method="extended_stabilizer")

ansatz = EfficientSU2(1)
print(ansatz.decompose())

num_parameters = ansatz.num_parameters
parameters = np.random.rand(num_parameters)
parameters = np.reshape(parameters, (-1, num_parameters)).tolist()
batch_size = len(parameters)

# RUN
try:
# This does not work ...
    _ansatz = transpile(ansatz, backend=backend, basis_gates=backend.configuration().basis_gates, optimization_level=2)
    print(_ansatz.decompose())
    job = backend.run(_ansatz, parameter_binds=[{k: [0] for k in _ansatz.parameters}])
    print(job.result())
except:
# ... however this does
    print(ansatz.decompose())
    job = backend.run(ansatz.decompose(), parameter_binds=[{k: [0] for k in _ansatz.parameters}])
    print(job.result())

# PRIMITIVE
estimator = aer_estimator(
    backend_options=backend.options._fields,
    transpile_options={"optimization_level": 2},
    run_options=None,
    approximation=False,
    skip_transpilation=False,
)

try:
    print("Estimator 1")
    # This does not work ...
    operator = "X"
    job = estimator.run(batch_size * [ansatz], batch_size * [operator], parameters)
    print(job.result())
except:
    print("Estimator 2")
    # neither this ...
    job = estimator.run(batch_size * [ansatz.decompose()], batch_size * [operator], parameters)
    print(job.result())
finally:
    # nor this ...
    print("Estimator 3")
    estimator = aer_estimator(
        backend_options=backend.options._fields,
        transpile_options={"optimization_level": 3},
        run_options=None,
        approximation=False,
        skip_transpilation=True,
    )
    _ansatz = transpile(ansatz, backend=backend, basis_gates=backend.configuration().basis_gates, optimization_level=2)
    job = estimator.run(batch_size * [_ansatz], batch_size * [operator], parameters)
    print(job.result())

What is the expected behavior?

All of the above cases should run without any problem.

Suggested solutions

In all failure cases, the problem appears when trying to get the global phase of the circuit at a point when the parameters are unbound:

File "qiskit_aer/backends/aer_compiler.py", line 376, in assemble_circuit
    global_phase = float(circuit.global_phase)
  File "/qiskit/circuit/parameterexpression.py", line 477, in __float__
    raise TypeError(
TypeError: ParameterExpression with unbound parameters ({ParameterVectorElement(θ[5]), ParameterVectorElement(θ[1]), ParameterVectorElement(θ[6]), ParameterVectorElement(θ[7]), ParameterVectorElement(θ[2]), ParameterVectorElement(θ[3]), ParameterVectorElement(θ[4]), ParameterVectorElement(θ[0])}) cannot be cast to a float.

So probably it needs to be done before explicitly, note however, this would not explain why with other methods is working.

@hhorii hhorii self-assigned this Apr 17, 2023
@hhorii hhorii added this to the Aer 0.12.1 milestone Apr 17, 2023
@hhorii
Copy link
Collaborator

hhorii commented Apr 18, 2023

Thank you for your report. @ikkoham thankfully identified the root cause of this issue with a small test case as follows:

from qiskit import QuantumCircuit
from qiskit.circuit import Parameter
from qiskit_aer import AerSimulator

backend = AerSimulator(method="stabilizer")
a = Parameter("a")
qc = QuantumCircuit(1)
qc.global_phase = a
print(qc)
backend.run(qc).result()

In transpliation with extended_stabilizer method, this global _phase is modified with parameterized values, but current Aer does not have implementation for this parameterization. I guess that this bug is since Aer began :-). Thank you for your reporting.

@hhorii hhorii added bug Something isn't working duplicate This issue or pull request already exists labels May 15, 2023
@hhorii
Copy link
Collaborator

hhorii commented May 31, 2023

#1814 closes this issue.

@hhorii hhorii closed this as completed May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants