Some classical bits are not used for measurements. the number of classical bits (2), the used classical bits (set())

189 views Asked by At

I am looking to work with loops in quantum computing, but when I run my circuit I get this error: Some classical bits are not used for measurements. the number of classical bits (2), the used classical bits (set()).

qubits = QuantumRegister(1)
clbits = ClassicalRegister(1)
circuit = QuantumCircuit(qubits, clbits)
(q0,) = qubits
(c0,) = clbits
 
with circuit.for_loop(range(5)) as _:
    circuit.x(q0)
circuit.measure(q0, c0)
circuit.draw("mpl")

result = Sampler().run(circuit).result()
result.quasi_dists[0] 
# example output counts: {'1': 1024}

QiskitError: 'Some classical bits are not used for measurements. the number of classical bits (2), the used classical bits (set()).'

1

There are 1 answers

0
Engr. Khuram  Shahzad On BEST ANSWER

When utilizing the Primitive in Qiskit, such as the Sampler, it is necessary to measure all classical bits before obtaining the results. However, in the provided code, there are classical bits that are not included in the measurement process. In certain scenarios, it may not be necessary to measure all qubits.

It has two cases for solution:

  1. Make measurements on all classical bits.
  2. Use IBM-backed instead of Primitive

Use IBM backed instead if Primitive: When using IBM's backend, there is no requirement that all classical bits should be involved in measurement.

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, Aer, execute, transpile,assemble from qiskit_ibm_provider import IBMProvider

hub = "ibm-q"
group = "open"
project = "main"
backend_name = "ibmq_qasm_simulator"
hgp = f"{hub}/{group}/{project}"

# Note: If you do not have your IBM Token saved, run the following
# More info: https://quantum-computing.ibm.com/lab/docs/iql/manage/account/ibmq
# import warnings
# warnings.filterwarnings('ignore')
# IBMProvider.save_account("TOKEN", overwrite=True)
# IBMProvider.load_account()
# IBMProvider.providers()
# load provider and backend
provider = IBMProvider()
backend = provider.get_backend(backend_name, instance=hgp)
print(f"Using backend {backend.name}")
qasm3_backends = set(
    backend.name for backend in provider.backends(dynamic_circuits=True)
)
 
print(f"The following backends support dynamic circuits: {qasm3_backends}")



qubits = QuantumRegister(2)
clbits = ClassicalRegister(2)
circuit = QuantumCircuit(qubits, clbits)
 
q0, q1 = qubits
c0, c1 = clbits
 
circuit.h([q0, q1])
circuit.measure(q0, c0)
circuit.measure(q1, c1)
with circuit.while_loop((clbits, 0b11)):
    circuit.h([q0, q1])
    circuit.measure(q1, c1)
 
circuit.draw("mpl")

job = backend.run(circuit, shots=20000, dynamic=True)
job.job_id()