Qiskit / qiskit-aer

Aer is a high performance simulator for quantum circuits that includes noise models
https://qiskit.github.io/qiskit-aer/
Apache License 2.0
480 stars 354 forks source link

Parameter binds does not work when unrolling parameterized gates #1346

Closed salperinlea closed 2 years ago

salperinlea commented 2 years ago

Informations

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 Qiskit/qiskit-aer#1249.

chriseclectic commented 2 years ago

@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