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
483 stars 359 forks source link

`AerEstimator` fails with a circuit with classical bits #1682

Closed tula3and closed 1 year ago

tula3and commented 1 year ago

Informations

What is the current behavior?

If I run the circuit with classical bits on Aer estimator, it returns the error below:

Traceback (most recent call last):
  Input In [28] in <cell line: 29>
    result = job.result()
  File /opt/conda/lib/python3.8/site-packages/qiskit/primitives/primitive_job.py:50 in result
    return self._future.result()
  File /opt/conda/lib/python3.8/concurrent/futures/_base.py:437 in result
    return self.__get_result()
  File /opt/conda/lib/python3.8/concurrent/futures/_base.py:389 in __get_result
    raise self._exception
  File /opt/conda/lib/python3.8/concurrent/futures/thread.py:57 in run
    result = self.fn(*self.args, **self.kwargs)
  File /opt/conda/lib/python3.8/site-packages/qiskit_aer/primitives/estimator.py:139 in _call
    return self._compute(circuits, observables, parameter_values, run_options)
  File /opt/conda/lib/python3.8/site-packages/qiskit_aer/primitives/estimator.py:220 in _compute
    experiment.add_register(creg)
  File /opt/conda/lib/python3.8/site-packages/qiskit/circuit/quantumcircuit.py:1440 in add_register
    raise CircuitError('register name "%s" already exists' % register.name)
CircuitError: 'register name "c" already exists'

If I run the code on the Terra estimator, it doesn't return the error.

Steps to reproduce the problem

I run the code below which returns the error above.

from qiskit import QuantumCircuit 
from qiskit.circuit import Parameter
from qiskit.quantum_info import SparsePauliOp
from qiskit_aer.primitives import Estimator
import numpy as np

theta = Parameter('theta')

circuit = QuantumCircuit(2,1)
circuit.x(1)
circuit.h(0)
circuit.cp(theta,0,1)
circuit.h(0)

observable = SparsePauliOp("ZZ")
estimator = Estimator()

phases = np.linspace(0, 2*np.pi, 50)

# phases need to be expressed as a list of lists in order to work
parameter_values = [[phase] for phase in phases]

# prepare circuits and observables
circuits = [circuit] * len(phases)
observables = [observable] * len(phases)

# run estimator
job = estimator.run(circuits, observables, parameter_values)
result = job.result()

print(f"  > Observable: {observable.paulis}")
print(f"  > Parameter values: {parameter_values}")
print(f"  > Expectation value: {result.values[0]}")
print(f"  > Metadata: {result.metadata[0]}")

What is the expected behavior?

No error like the result of Terra estimator.

Suggested solutions

jakelishman commented 1 year ago

@ikkoham, @t-imamichi: please could one of you look at this, or delegate it to somebody?

t-imamichi commented 1 year ago

Estimator is originally designed to accept circuit without classical bits and measurements. Here is a discussion. https://github.com/Qiskit/qiskit-terra/pull/9692

hhorii commented 1 year ago

Please let me close this issue because of no response in more than 2 months.