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

Sampler measurement assumptions #10706

Open
1ucian0 opened this issue Aug 24, 2023 · 2 comments
Open

Sampler measurement assumptions #10706

1ucian0 opened this issue Aug 24, 2023 · 2 comments

Comments

@1ucian0
Copy link
Member

1ucian0 commented Aug 24, 2023

@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.

@t-imamichi
Copy link
Member

As for the first one, we introduced the error 'Some classical bits are not used..." for the simplicity of the implementation. There seem to be some complicated corner cases according to the following discussion though I don't remember the details. @ikkoham may remember it.
https://github.com/Qiskit/qiskit/pull/7780/files#r833295071
This error message is included only for the reference implementation Sampler. BaseSampler does not have it.

As for the second one, we check measurements at BaseSampler. So, the validation is done by Sampler of qiskit-ibm-runtime too.
If we allow a circuit with no measurements to run, users can submit the job containing both circuits w/ and w/o measurements, wait for a queue (can be a long time), and receive an empty result for the circuits w/o measurements. I don't have a strong opinion of early error message or empty result for circuits w/o measurements.

@ikkoham
Copy link
Contributor

ikkoham commented Aug 30, 2023

#10642 changes the error message of the first code example: ValueError: The 0-th circuit does not have Measure instruction. Without measurements, the circuit cannot be sampled from.

For the second one, that issue comment is all. Thank you @t-imamichi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants