Qiskit / qiskit-ibm-runtime

IBM Client for Qiskit Runtime
https://docs.quantum.ibm.com/api/qiskit-ibm-runtime
Apache License 2.0
161 stars 157 forks source link

Allow for iterating through result data #1971

Closed nonhermitian closed 1 month ago

nonhermitian commented 1 month ago

What is the expected feature or enhancement?

Currently one has to know the name of the classical register in order to extract values from a data object. This is not ideal. Namely, the following circuits do the same thing, have no user input into the naming of the registers, but need to have the their corresponding data accessed differently.


qc = QuantumCircuit(5,5)
qc.h(2)
qc.cx(2,1)
qc.cx(1,0)
qc.cx(2,3)
qc.cx(3,4)
qc.measure(range(5), range(5))

qc2 = QuantumCircuit(5)
qc2.h(2)
qc2.cx(2,1)
qc2.cx(1,0)
qc2.cx(2,3)
qc2.cx(3,4)
qc2.measure_all()

Unless I know the register names, I cannot access the data. Currently there are two ways to know these values without having the circuit available. One is the private fields method, data._FIELDS, and the other is iterating over the data object itself, which returns str values for the registers. However, as an user I rarely care about the register name itself, it is a free variable that does not change the computation, but rather just want access to the data, most commonly from a single register.

Since only the register order matters, it would be nice if one could iterate through the data object and obtain the data directly without knowing the register names.

Acceptance criteria For the above examples it should be possible to extract that BitArray data for the registers for both circuits in a manner such as:

job.result()[0].data[0]

that is independent of the underlying register name.

ihincks commented 1 month ago

@nonhermitian I'm wondering if the currently available behaviour meets your needs?

data = list(job.result()[0].data.values())

In short, .data here is mapping type, so you can do whatever you usually do with mapping types, including getting all the names as list(job.result()[0].data).

(Also, btw, this is technically a qiskit-core issue because that's where primitive types/abstraction are defined.)

nonhermitian commented 1 month ago

Ok thanks! I think that probably does the same thing that I would want. Since I was looking to wrap runtime to get backend.run() back I ended up using item.join_data().get_counts() (which I was also unaware of until recent) to combine and then chunk it based on the num_bits.