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
464 stars 354 forks source link

stabilizer method fails with SamplerV2 #2164

Closed kevinsung closed 3 weeks ago

kevinsung commented 3 weeks ago

Informations

What is the current behavior?

stabilizer method fails with SamplerV2 even though it works with V1 Sampler.

Steps to reproduce the problem

import numpy as np
from qiskit.circuit.library import EfficientSU2
from qiskit_aer.primitives import Sampler, SamplerV2

n_qubits = 8
circuit = EfficientSU2(n_qubits)
circuit.measure_all()
rng = np.random.default_rng(1234)
params = rng.choice(
    [0, np.pi / 2, np.pi, 3 * np.pi / 2],
    size=circuit.num_parameters,
)

# works
sampler = Sampler(backend_options=dict(method="stabilizer"))
job = sampler.run(circuit.decompose(), params, shots=10)
quasis = job.result().quasi_dists[0]

# throws error
samplerv2 = SamplerV2(options=dict(backend_options=dict(method="stabilizer")))
pub = (circuit.decompose(), params, 10)
job = samplerv2.run([pub])
result = job.result()

QiskitError: 'ERROR:  [Experiment 0] Circuit EfficientSU2 contains invalid instructions {"gates": {ry}} for "stabilizer" method.Circuit EfficientSU2 contains invalid parameters  for "stabilizer" method. ,  ERROR: Circuit EfficientSU2 contains invalid instructions {"gates": {ry}} for "stabilizer" method.Circuit EfficientSU2 contains invalid parameters  for "stabilizer" method.'

What is the expected behavior?

Should work

Suggested solutions

doichanj commented 3 weeks ago

I think V2 primitives only accepts ISA circuits, so the circuits should be transpiled before passing to run() function (V1 primitive runs because it calls transpiler inside)

kevinsung commented 3 weeks ago

How do I obtain the appropriate transpiler target from the SamplerV2 instance?

doichanj commented 3 weeks ago

circuit = transpile(circuit, AerSimulator(method='stabilizer')) Do you think it is acceptable for users, or not?

kevinsung commented 3 weeks ago

It's less than ideal that the user has to initialize the AerSimulator object separately from the SamplerV2. This is not easy to discover.

doichanj commented 3 weeks ago

I do not know how users use qiskit.primitives with transpiler but I think we have to provide the similar way to use qiskit_aer.primitives

kevinsung commented 3 weeks ago

With qiskit.primitives, it seems that no transpilation is required. It can even handle the EfficientSU2 gate directly (for qiskit_aer.primitives I had to do circuit.decompose() to decompose that gate.

For example, this works:

import numpy as np
from qiskit.circuit.library import EfficientSU2
from qiskit.primitives import StatevectorSampler

n_qubits = 8
circuit = EfficientSU2(n_qubits)
circuit.measure_all()
rng = np.random.default_rng(1234)
params = rng.choice(
    [0, np.pi / 2, np.pi, 3 * np.pi / 2],
    size=circuit.num_parameters,
)

samplerv2 = StatevectorSampler()
pub = (circuit, params, 10)
job = samplerv2.run([pub])
result = job.result()
kevinsung commented 3 weeks ago

To match this behavior, the primitives in Qiskit Aer should not require any transpilation. It should handle any gate that can be simulated.

kevinsung commented 3 weeks ago

Closing in favor of #2165