quri_parts.braket.backend.saved_sampling module

class BraketSavedDataSamplingResult(raw_data)

Bases: SamplingResult

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

The raw_data should take in the output of braket_task.result().measurement_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 BraketSavedDataSamplingJob(circuit_program_str, n_shots, saved_result)

Bases: SamplingJob

An object that represents a saved sampling job.

Parameters:
  • circuit_program_str (str) – A string that represents the circuit used in a sampling job. Note that it should take in the program string of a braket quantum circuit. It can be accessed by braket_circuit.to_ir().json().

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

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

circuit_program_str: str
n_shots: int
saved_result: BraketSavedDataSamplingResult
result()

Returns the result of the sampling job.

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

Return type:

BraketSavedDataSamplingResult

class BraketSavedDataSamplingBackend(device, saved_data, circuit_converter=<function convert_circuit>, circuit_transpiler=<quri_parts.braket.circuit.BraketSetTranspiler object>, enable_shots_roundup=True, qubit_mapping=None, run_kwargs={})

Bases: SamplingBackend

A Braket backend for replaying saved sampling experiments. When a sampler is created with a BraketSavedDataSamplingBackend 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 = LocalSimulator()
>>> sampling_backend = BraketSamplingBackend(
...     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 = BraketSavedDataSamplingBackend(
...     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:
  • device (Device) – A Braket braket.devices.Device for circuit execution.

  • saved_data (str) – A json string output by the .jobs_json property of :class:`~quri_parts.braket.backend.BraketSamplingBackend.

  • circuit_converter (BraketCircuitConverter) – A function converting ImmutableQuantumCircuit to a Braket braket.circuits.Circuit.

  • circuit_transpiler (Optional[CircuitTranspiler]) – A transpiler applied to the circuit before running it. BraketSetTranspiler 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 braket.devices.Device.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[BraketSavedDataSamplingJob]

encode_saved_data_job_sequence_to_json(saved_data_seq)
Parameters:

saved_data_seq (Sequence[BraketSavedDataSamplingJob])

Return type:

str