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

Parameter binds does not work when unrolling parameterized gates #1346

Closed
salperinlea opened this issue Sep 16, 2021 · 1 comment · Fixed by #1356
Closed

Parameter binds does not work when unrolling parameterized gates #1346

salperinlea opened this issue Sep 16, 2021 · 1 comment · Fixed by #1356
Labels
bug Something isn't working stable-backport-potential The issue or PR might be minimal and/or import enough to backport to stable

Comments

@salperinlea
Copy link

Informations

  • Qiskit Aer version: 0.9
  • Python version: 3.8.11
  • Operating system: Ubuntu 18.04 lts

What is the current behavior?

parameters must be manually bound beforehand or else noisy simulations cannot be executed.
One of three errors arises, which will be different depending on what you pass as a parameter bind. If binds are List of Dict(Parameter, number), the following error occurs:


Simulation failed and returned the following error message:
ERROR: Failed to load qobj: [json.exception.type_error.302] type must be array, but is number

Whereas, if one attempts to rectify this with binds as List of Dict(Parameter,array), one gets either

Simulation failed and returned the following error message:
ERROR: Failed to load qobj: Invalid parameterized qobj: parameterization value out of range

if using np.asarray([number]). if using np.array(number), one instead gets:

Simulation failed and returned the following error message:
ERROR: Failed to load qobj: Invalid number of dimensions!

Steps to reproduce the problem

import qiskit
from qiskit import QuantumCircuit, execute, BasicAer
import numpy as np
from qiskit.providers.aer.noise import NoiseModel
from qiskit.providers.aer.noise import pauli_error

a=qiskit.circuit.Parameter('a')
binds={a:np.array([np.pi])}
qc = QuantumCircuit(1, 1)
qc.rx(a,0)
qc.measure(0,0)

p_gate1 = 0.1
error_gate1 = pauli_error([('X',p_gate1), ('I', 1 - p_gate1)])
n= NoiseModel()
n.add_all_qubit_quantum_error(error_gate1,["u1", "u2", "u3",'x'])
sim_noise = qiskit.providers.aer.QasmSimulator(noise_model=n)
t=qiskit.transpile(qc,sim_noise)
job = execute(experiments=t,
              parameter_binds=binds,
              backend=sim_noise,noise_model=n,shots=1000)
print(job.result().get_counts())

What is the expected behavior?

Parameter binds of List of Dict(Parameter,number) should be acceptable as a pass-down.

Suggested solutions

No clue. The fix to one problem keeps creating a new one. This is potentially related to the fix to #1249.

@chriseclectic
Copy link
Member

@mtreinish It looks to be something to do with transpiling to different basis gates, though I'm not sure why this would cause an issue. Here is a more minimal example that avoids the execute function.

import numpy as np
from qiskit import QuantumCircuit, transpile
from qiskit.circuit import Parameter
from qiskit.providers.aer import AerSimulator

a = Parameter('a')
qc = QuantumCircuit(1, 1)
qc.rx(a,0)
qc.measure(0,0)

# Transpile rx -> u3
tqc = transpile(qc, basis_gates=['u3'])

binds = [{a: np.array([np.pi])}]
result = AerSimulator().run(tqc, parameter_binds=binds).result()
Simulation failed and returned the following error message:
ERROR: Failed to load qobj: Invalid parameterized qobj: parameterization value out of range

@chriseclectic chriseclectic changed the title Noisy Simulations require manual parameter binding; multiple errors encountered. Parameter binds does not work when unrolling parameterized gates Sep 23, 2021
@mtreinish mtreinish added the stable-backport-potential The issue or PR might be minimal and/or import enough to backport to stable label Sep 23, 2021
mtreinish added a commit to mtreinish/qiskit-aer that referenced this issue Sep 23, 2021
This commit fixes an oversight in the handling of the parameter binds
kwarg on backend.run(). To build the parameterizations representation
that aer's c++ code expects we need to iterate over all the instructions
to find any that are parameterized. In this check we were incorrectly
finding gates with ParameterExpressions that were already bound to a
value (and had no unbound parameters). This would often come up from the
transpiler when the basis translator uses a parameter expression to
adjust rotation angles when translating between 1q gates. This commit
fixes this by adding a check for fully bound parameter expressions and
not including them in the set of parameterizations we pass to the inner
simulator code.

Fixes Qiskit#1346
chriseclectic pushed a commit that referenced this issue Sep 23, 2021
This commit fixes an oversight in the handling of the parameter binds
kwarg on backend.run(). To build the parameterizations representation
that aer's c++ code expects we need to iterate over all the instructions
to find any that are parameterized. In this check we were incorrectly
finding gates with ParameterExpressions that were already bound to a
value (and had no unbound parameters). This would often come up from the
transpiler when the basis translator uses a parameter expression to
adjust rotation angles when translating between 1q gates. This commit
fixes this by adding a check for fully bound parameter expressions and
not including them in the set of parameterizations we pass to the inner
simulator code.

Fixes #1346
chriseclectic pushed a commit to chriseclectic/qiskit-aer that referenced this issue Oct 5, 2021
This commit fixes an oversight in the handling of the parameter binds
kwarg on backend.run(). To build the parameterizations representation
that aer's c++ code expects we need to iterate over all the instructions
to find any that are parameterized. In this check we were incorrectly
finding gates with ParameterExpressions that were already bound to a
value (and had no unbound parameters). This would often come up from the
transpiler when the basis translator uses a parameter expression to
adjust rotation angles when translating between 1q gates. This commit
fixes this by adding a check for fully bound parameter expressions and
not including them in the set of parameterizations we pass to the inner
simulator code.

Fixes Qiskit#1346
chriseclectic pushed a commit to chriseclectic/qiskit-aer that referenced this issue Oct 8, 2021
This commit fixes an oversight in the handling of the parameter binds
kwarg on backend.run(). To build the parameterizations representation
that aer's c++ code expects we need to iterate over all the instructions
to find any that are parameterized. In this check we were incorrectly
finding gates with ParameterExpressions that were already bound to a
value (and had no unbound parameters). This would often come up from the
transpiler when the basis translator uses a parameter expression to
adjust rotation angles when translating between 1q gates. This commit
fixes this by adding a check for fully bound parameter expressions and
not including them in the set of parameterizations we pass to the inner
simulator code.

Fixes Qiskit#1346
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stable-backport-potential The issue or PR might be minimal and/or import enough to backport to stable
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants