Skip to content

Amazon Braket

Braket Digital backend

Backend dataclass

Bases: Backend

assign_parameters(circuit, param_values)

Assign numerical values to the circuit parameters

Source code in qadence/backends/braket/backend.py
def assign_parameters(
    self, circuit: ConvertedCircuit, param_values: dict[str, Tensor | float]
) -> BraketCircuit:
    """Assign numerical values to the circuit parameters"""
    if param_values is None:
        return circuit.native()

    params_copy = param_values.copy()
    pnames = [p.name for p in circuit.native.parameters]

    # account for fixed parameters
    for name in param_values.keys():
        if name not in pnames:
            params_copy.pop(name)

    # make sure that all the parameters are single floats
    # otherwise it won't be accepted by Braket
    native_params = promote_parameters(params_copy)

    # assign the parameters to the circuit
    assigned_circuit = circuit.native(**native_params)

    return assigned_circuit

run(circuit, param_values={}, state=None, endianness=Endianness.BIG)

Execute the circuit and return a wavefunction in form of a statevector.

PARAMETER DESCRIPTION
circuit

The circuit that is executed.

TYPE: ConvertedCircuit

param_values

Parameters of the circuit (after calling the embedding function on the user-facing parameters).

TYPE: dict[str, Tensor] DEFAULT: {}

state

Initial state.

TYPE: Tensor | None DEFAULT: None

endianness

The endianness of the wave function.

TYPE: Endianness DEFAULT: BIG

Source code in qadence/backends/braket/backend.py
def run(
    self,
    circuit: ConvertedCircuit,
    param_values: dict[str, Tensor] = {},
    state: Tensor | None = None,
    endianness: Endianness = Endianness.BIG,
) -> Tensor:
    """
    Execute the circuit and return a wavefunction in form of a statevector.

    Arguments:
        circuit: The circuit that is executed.
        param_values: Parameters of the circuit (after calling the embedding
            function on the user-facing parameters).
        state: Initial state.
        endianness: The endianness of the wave function.
    """

    if state is not None:
        raise NotImplementedError

    if self.is_remote:
        # handle here, or different backends?
        raise NotImplementedError

    # loop over all values in the batch
    results = []
    for vals in to_list_of_dicts(param_values):
        final_circuit = self.assign_parameters(circuit, vals)

        final_circuit.state_vector()  # set simulation type
        task = self._device.run(final_circuit, 0)
        results.append(task.result().values[0])
    states = torch.tensor(np.array(results))

    n_qubits = circuit.abstract.n_qubits
    if endianness != self.native_endianness and n_qubits > 1:
        from qadence.transpile import invert_endianness

        states = invert_endianness(states)
    return states

sample(circuit, param_values={}, n_shots=1, state=None, endianness=Endianness.BIG)

Execute the circuit and return samples of the resulting wavefunction.

Source code in qadence/backends/braket/backend.py
def sample(
    self,
    circuit: ConvertedCircuit,
    param_values: dict[str, Tensor] = {},
    n_shots: int = 1,
    state: Tensor | None = None,
    endianness: Endianness = Endianness.BIG,
) -> list[Counter]:
    """Execute the circuit and return samples of the resulting wavefunction."""
    if state is not None:
        raise NotImplementedError("Braket cannot handle a custom initial state.")

    if n_shots < 1:
        raise ValueError("You can only call sample with n_shots>0.")

    if self.is_remote:
        # handle here, or different backends?
        raise NotImplementedError

    # loop over all values in the batch
    samples = []
    for vals in to_list_of_dicts(param_values):
        final_circuit = self.assign_parameters(circuit, vals)
        task = self._device.run(final_circuit, n_shots)
        samples.append(task.result().measurement_counts)
    if endianness != self.native_endianness:
        from qadence.transpile import invert_endianness

        samples = invert_endianness(samples)
    return samples