mimiqcircuits.qcsresults¶
Classes
|
Results of a circuit execution. |
- class QCSResults(simulator=None, version=None, fidelities=None, avggateerrors=None, cstates=None, zstates=None, amplitudes=None, timings=None)[source]¶
Bases:
objectResults of a circuit execution.
Remote executions (
get_result/get_resultson a connection) and local backends return this object. Every argument is optional and stored as an attribute of the same name; missing lists and dictionaries start empty.- Parameters:
simulator (str, optional) – The name of the simulator.
version (str, optional) – The version of the simulator.
fidelities (list, optional) – One fidelity estimate per execution of the circuit.
avggateerrors (list, optional) – One average multi-qubit gate error estimate per execution.
cstates (list[BitString], optional) – The sampled classical registers, one
BitStringper sample.zstates (list, optional) – The sampled complex-valued
zregisters, one sequence of numbers per sample.amplitudes (dict, optional) – Statevector amplitudes, mapping a
BitStringto a complex number.timings (dict, optional) – Time in seconds spent in each phase of the computation, keyed by phase name (for example
"total").
Examples
Results normally come from a connection:
>>> from mimiqcircuits import * >>> conn = MimiqConnection() >>> conn.connect() >>> c = Circuit() >>> c.push(GateH(), 0) 1-qubit circuit with 1 instruction: └── H @ q[0] >>> c.push(Measure(), 0, 0) 1-qubit, 1-bit circuit with 2 instructions: ├── H @ q[0] └── M @ q[0], c[0] >>> job = conn.submit(c, nsamples=1000) >>> res = conn.get_result(job)
Here we build one by hand to show how to inspect it: five samples of a two-bit register and one amplitude.
>>> res = QCSResults( ... simulator="MIMIQ-StateVector", ... version="1.0", ... fidelities=[1.0], ... avggateerrors=[0.0], ... cstates=[BitString("00"), BitString("11"), BitString("11"), ... BitString("00"), BitString("11")], ... amplitudes={BitString("00"): 0.7071 + 0j}, ... timings={"total": 0.0012, "sample": 0.0002}, ... )
The printed summary shows timings, fidelity, and the most sampled states.
>>> res QCSResults: ├── simulator: MIMIQ-StateVector 1.0 ├── timings: │ ├── sample time: 200μs │ └── total time: 1.2ms ├── fidelity estimate: 1 ├── average multi-qubit gate error estimate: 0 ├── most sampled: │ ├── bs"11" => 3 │ └── bs"00" => 2 ├── 1 executions ├── 1 amplitudes └── 5 samples
The raw data is available as attributes.
>>> len(res.cstates) 5 >>> res.cstates[0] bs"00" >>> res.amplitudes[BitString("00")] (0.7071+0j)
histogram()counts how often each state was sampled.>>> res.histogram() {bs"00": 2, bs"11": 3}
- histogram()[source]¶
Count how often each classical state was sampled.
- Returns:
A mapping from each sampled
BitStringincstatesto its number of occurrences (int), in order of first appearance.- Return type:
Examples
>>> from mimiqcircuits import * >>> res = QCSResults(cstates=[BitString("01"), BitString("10"), BitString("01")]) >>> res.histogram() {bs"01": 2, bs"10": 1}
- histzvars()[source]¶
Count how often each
zregister state was sampled.- Returns:
A mapping from each state in
zstates, converted to a tuple, to its number of occurrences (int), in order of first appearance.- Return type:
Examples
>>> from mimiqcircuits import * >>> res = QCSResults(zstates=[[1.0], [0.5], [1.0]]) >>> res.histzvars() {(1.0,): 2, (0.5,): 1}
- saveproto(file)[source]¶
Save the results to a protobuf file.
- Parameters:
file (str or file-like) – A path, or a binary file object with a
writemethod.- Returns:
The number of bytes written.
- Return type:
- Raises:
ValueError – If the file cannot be written.
Examples
>>> from mimiqcircuits import * >>> import os, tempfile >>> res = QCSResults( ... simulator="MIMIQ-StateVector", ... version="1.0", ... cstates=[BitString("01"), BitString("01"), BitString("10")], ... )
Write the results to a file, then read them back with the static method
loadproto().>>> path = os.path.join(tempfile.mkdtemp(), "results.pb") >>> nbytes = res.saveproto(path) >>> loaded = QCSResults.loadproto(path) >>> loaded.histogram() == res.histogram() True
- static loadproto(file)[source]¶
Load results from a protobuf file written by
saveproto().This is a static method: call it on the class.
- Parameters:
file (str or file-like) – A path, or a binary file object with a
readmethod.- Returns:
The loaded results.
- Return type:
- Raises:
ValueError – If the file cannot be read or parsed.
See also
saveproto(), which has an example.