-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
@blakejohnson raises a good point on the reference implementation for the Sampler in https://github.com/Qiskit/qiskit/pull/10584/files#r1294545780 . Let's move that conversation here.
When a circuit does not have measurements:
Currently, if a circuit has no measurements (but with classical wires), the reference Sampler
raises an exception Some classical bits are not used for measurements.
from qiskit import QuantumCircuit
from qiskit.primitives.sampler import Sampler
qc = QuantumCircuit(3 ,3)
qc.h(0)
qc.cx(0,1)
qc.cx(0,2)
job = Sampler().run(qc)
QiskitError: 'Some classical bits are not used for measurements. the number of classical bits (3), the used classical bits (set()).'
It makes sense to convert this Error into a warning and return the state of all the classical wires. The reference implementation might assume that the initial state of the classical wires is 0
(this needs to be documented) and return {'000':1}
. This would allow to partially measure a circuit. That is, if a not all the wires are measured, it could still run (with the warning, since that might not be intended)
This does not work currently:
from qiskit import QuantumCircuit
from qiskit.primitives.sampler import Sampler
qc = QuantumCircuit(3, 3)
qc.h(0)
qc.cx(0,1)
qc.cx(0,2)
qc.measure(2, 0)
job = Sampler().run(qc)
print(job.result().quasi_dists)
When a circuit does not have classical wires:
Currently, if a circuit has no classical bits, the reference Sampler raises an exception circuit does not have any classical bit
:
from qiskit import QuantumCircuit
from qiskit.primitives.sampler import Sampler
qc = QuantumCircuit(3)
qc.h(0)
qc.cx(0,1)
qc.cx(0,2)
job = Sampler().run(qc)
ValueError: The 0-th circuit does not have any classical bit. Sampler requires classical bits, plus measurements on the desired qubits.
Again, here would probably make sense to run (converting this exception into a warning) and return {}
. Because the case for "this is probably a user-error" in this context is stronger, maybe keeping the error makes sense (as a QiskitError
instead?). I dump it here for completeness.