quri_parts.qiskit.backend.saved_sampling module#

class QiskitSavedDataSamplingResult(raw_data)#

Bases: SamplingResult

An object that holds a sampling count from qiskit backend output and converts it into quri-parts sampling count.

The raw_data should take in the output of qiskit_result.get_counts(), which is a counter that uses str as its key.

Parameters:

raw_data (dict[str, int]) –

raw_data: dict[str, int]#
property counts: backend.SamplingCounts#

Convert the raw data to quri-parts sampling count.

The quri-parts sampling count is a counter that uses int as its key.

class QiskitRuntimeSavedDataSamplingResult(quasi_dist, n_shots)#

Bases: SamplingResult

An object that holds quasi disttribution and total shot count from qiskit runtime output and converts it into quri-parts sampling count.

Parameters:
  • quasi_dist (dict[int, float]) – The first element of the quasi_dists attribute of a qiskit.primitives.SamplerResult object.

  • n_shots (int) – The metadata[0][“shots”] output of a qiskit.primitives.SamplerResult object.

quasi_dist: dict[int, float]#
n_shots: int#
property counts: backend.SamplingCounts#

Measurement counts obtained by a sampling measurement.

class QiskitSavedDataSamplingJob(circuit_qasm, n_shots, saved_result)#

Bases: SamplingJob

An object that represents a saved sampling job.

Parameters:
  • circuit_qasm (str) – A string that represents the circuit used in a sampling job. Note that it should take in the qasm string of a qiskit quantum circuit. It can be accessed by qiskit.qasm3.dumps(qiskit_circuit).

  • n_shots (int) – The total shots of a sampling job.

  • saved_result (QiskitSavedDataSamplingResult | QiskitRuntimeSavedDataSamplingResult) – A QiskitSavedDataSamplingResult instance that represents the result when (circuit_str, n_shots) is passed into the sampler.

circuit_qasm: str#
n_shots: int#
saved_result: QiskitSavedDataSamplingResult | QiskitRuntimeSavedDataSamplingResult#
result()#

Returns the result of the sampling job.

If the job is not complete, this method waits until the job finishes.

Return type:

QiskitSavedDataSamplingResult | QiskitRuntimeSavedDataSamplingResult

class QiskitSavedDataSamplingBackend(backend, saved_data, circuit_converter=<function convert_circuit>, circuit_transpiler=None, enable_shots_roundup=True, qubit_mapping=None, run_kwargs={})#

Bases: SamplingBackend

A Qiskit backend for replaying saved sampling experiments. When a sampler is created with a QiskitSavedDataSamplingBackend object, the sequence of (circuit, n_shots) pairs should be passed in to the sampler the same order as the orginal experiment.

Example

  1. Sampling experiment

1-a: Sampling mode with data saving

>>> backend_device = AerSimulator()
>>> sampling_backend = QiskitSamplingBackend(
...     backend_device, save_data_while_sampling=True
... )
>>> sampler = create_sampler_from_sampling_backend(sampling_backend)

1-b: Perform sampling experiments

>>> sampling_count_1 = sampler(circuit_1, n_shots_1)
>>> sampling_count_2 = sampler(circuit_2, n_shots_2)
>>> sampling_count_3 = sampler(circuit_3, n_shots_3)

1-c: Dump sampling data

>>> experiment_json_str = sampling_backend.jobs_json()
  1. Replay sampling experiment

2-a: Create sampling backend and sampler with saved data.

>>> saved_data_sampling_backend = QiskitSavedDataSamplingBackend(
...     backend = backend_device,
...     saved_data = experiment_json_str
... )
>>> saved_data_sampler = create_sampler_from_sampling_backend(
...    saved_data_sampling_backend
... )

2-b: Replay sampling experiment.

(circuit, n_shots) pairs are passed in to the saved_data_sampler the same order as they were passed in to the sampler.

>>> replayed_sampling_count_1 = sampler(circuit_1, n_shots_1)
>>> replayed_sampling_count_2 = sampler(circuit_2, n_shots_2)
>>> replayed_sampling_count_3 = sampler(circuit_3, n_shots_3)
Parameters:
  • backend (Backend) – A Qiskit qiskit.providers.backend.Backend for circuit execution.

  • saved_data (str) – A json string output by the .json_str property of :class:`~quri_parts.qiskit.backend.QiskitSamplingBackend.

  • circuit_converter (QiskitCircuitConverter) – A function converting NonParametricQuantumCircuit to a Qiskit qiskit.circuit.QuantumCircuit.

  • circuit_transpiler (Optional[CircuitTranspiler]) – A transpiler applied to the circuit before running it. QiskitSetTranspiler is used when not specified.

  • enable_shots_roundup (bool) – If True, when a number of shots specified to sample() is smaller than the minimum number of shots supported by the device, it is rounded up to the minimum. In this case, it is possible that shots more than specified are used. If it is strictly not allowed to exceed the specified shot count, set this argument to False.

  • qubit_mapping (Optional[Mapping[int, int]]) – If specified, indices of qubits in the circuit are remapped before running it on the backend. It can be used when you want to use specific backend qubits, e.g. those with high fidelity. The mapping should be specified with “from” qubit indices as keys and “to” qubit indices as values. For example, if you want to map qubits 0, 1, 2, 3 to backend qubits as 0 → 4, 1 → 2, 2 → 5, 3 → 0, then the qubit_mapping should be {0: 4, 1: 2, 2: 5, 3: 0}.

  • run_kwargs (Mapping[str, Any]) – Additional keyword arguments for qiskit.providers.backend.Backend.run() method.

sample(circuit, n_shots)#

Perform a sampling measurement of a circuit.

Parameters:
Return type:

SamplingJob

decode_json_to_saved_data_sequence(json_str)#
Parameters:

json_str (str) –

Return type:

Sequence[QiskitSavedDataSamplingJob]

encode_saved_data_job_sequence_to_json(saved_data_seq)#
Parameters:

saved_data_seq (Sequence[QiskitSavedDataSamplingJob]) –

Return type:

str