ResultType
- class braket.circuits.result_type.ResultType(ascii_symbols)[source]
Bases:
objectClass
ResultTyperepresents a requested result type for the circuit. This class is considered the result type definition containing the metadata that defines what a requested result type is and what it does.- Parameters:
ascii_symbols (
list[str])
Initializes a
ResultType.- Parameters:
ascii_symbols (list[str]) – ASCII string symbols for the result type. This is used when printing a diagram of circuits.
- Raises:
ValueError –
ascii_symbolsisNone
- property ascii_symbols: list[str]
Returns the ascii symbols for the requested result type.
- Type:
list[str]
- property name: str
Returns the name of the result type
- Returns:
str – The name of the result type as a string
- to_ir(ir_type=IRType.JAQCD, serialization_properties=None, **kwargs)[source]
Returns IR object of the result type
- Parameters:
ir_type (IRType) – The IRType to use for converting the result type object to its IR representation. Defaults to IRType.JAQCD.
serialization_properties (SerializationProperties | None) – The serialization properties to use while serializing the object to the IR representation. The serialization properties supplied must correspond to the supplied
ir_type. Defaults to None.
- Return type:
Any- Returns:
Any – IR object of the result type
- Raises:
ValueError – If the supplied
ir_typeis not supported, or if the supplied serialization properties don’t correspond to their_type.
- copy(target_mapping=None, target=None)[source]
Return a shallow copy of the result type.
Note
If
target_mappingis specified, thenself.targetis mapped to the specified qubits. This is useful apply an instruction to a circuit and change the target qubits.- Parameters:
target_mapping (dict[QubitInput, QubitInput] | None) – A dictionary of qubit mappings to apply to the target. Key is the qubit in this
targetand the value is what the key is changed to. Default =None.target (QubitSetInput | None) – Target qubits for the new instruction.
- Return type:
ResultType
- Returns:
ResultType – A shallow copy of the result type.
- Raises:
TypeError – If both
target_mappingandtargetare supplied.
Examples
>>> result_type = ResultType.Probabilities(targets=[0]) >>> new_result_type = result_type.copy() >>> new_result_type.targets QubitSet(Qubit(0)) >>> new_result = result_type.copy(target_mapping={0: 5}) >>> new_result_type.target QubitSet(Qubit(5)) >>> new_result = result_type.copy(target=[5]) >>> new_result_type.target QubitSet(Qubit(5))
- classmethod register_result_type(result_type)[source]
Register a result type implementation by adding it into the
ResultTypeclass.- Parameters:
result_type (type[ResultType]) –
ResultTypeclass to register.- Return type:
None
- class AdjointGradient(observable, target=None, parameters=None)
Bases:
ObservableParameterResultTypeThe gradient of the expectation value of the provided observable, applied to target, with respect to the given parameter.
- Parameters:
observable (Observable)
target (list[QubitSetInput] | None)
parameters (list[str | FreeParameter] | None)
Inits an
AdjointGradient.- Parameters:
observable (Observable) – The expectation value of this observable is the function against which parameters in the gradient are differentiated.
target (list[QubitSetInput] | None) – Target qubits that the result type is requested for. Each term in the target list should have the same number of qubits as the corresponding term in the observable. Default is
None, which means the observable must operate only on 1 qubit and it is applied to all qubits in parallel.parameters (list[str | FreeParameter] | None) – The free parameters in the circuit to differentiate with respect to. Default:
all.
- Raises:
ValueError – If the observable’s qubit count does not equal the number of target qubits, or if
target=Noneand the observable’s qubit count is not 1.
Examples
>>> result_types.AdjointGradient(observable=observables.Z(0), parameters=["alpha", "beta"]) >>> result_types.AdjointGradient(observable=observables.Z(), target=0, parameters=["alpha", "beta"])
>>> tensor_product = observables.Y(0) @ observables.Z(1) >>> hamiltonian = observables.Y(0) @ observables.Z(1) + observables.H(0) >>> result_types.AdjointGradient( >>> observable=tensor_product, >>> parameters=["alpha", "beta"], >>> )
- static adjoint_gradient(observable, target=None, parameters=None)
Registers this function into the circuit class.
- Parameters:
observable (Observable) – The expectation value of this observable is the function against which parameters in the gradient are differentiated.
target (list[QubitSetInput] | None) – Target qubits that the result type is requested for. Each term in the target list should have the same number of qubits as the corresponding term in the observable. Default is
None, which means the observable must operate only on 1 qubit and it is applied to all qubits in parallel.parameters (list[str | FreeParameter] | None) – The free parameters in the circuit to differentiate with respect to. Default:
all.
- Return type:
ResultType
- Returns:
ResultType – gradient computed via adjoint differentiation as a requested result type
Examples
>>> alpha, beta = FreeParameter("alpha"), FreeParameter("beta") >>> circ = Circuit().h(0).h(1).rx(0, alpha).yy(0, 1, beta).adjoint_gradient( >>> observable=observables.Z(0), parameters=[alpha, beta] >>> )
- class Amplitude(state)
Bases:
ResultTypeThe amplitude of the specified quantum states as a requested result type. This is available on simulators only when
shots=0.- Parameters:
state (
list[str])
Initializes an
Amplitude.- Parameters:
state (list[str]) – list of quantum states as strings with “0” and “1”
- Raises:
ValueError – If state is
Noneor an empty list, or state is not a list of strings of ‘0’ and ‘1’
Examples
>>> result_types.Amplitude(state=["01", "10"])
- static amplitude(state)
Registers this function into the circuit class.
- Parameters:
state (list[str]) – list of quantum states as strings with “0” and “1”
- Return type:
- Returns:
ResultType – state vector as a requested result type
Examples
>>> circ = Circuit().amplitude(state=["01", "10"])
- property state: list[str]
- class DensityMatrix(target=None)
Bases:
ResultTypeThe full density matrix as a requested result type. This is available on simulators only when
shots=0.- Parameters:
target (QubitSetInput | None)
Inits a
DensityMatrix.- Parameters:
target (QubitSetInput | None) – The target qubits of the reduced density matrix. Default is
None, and the full density matrix is returned.
Examples
>>> result_types.DensityMatrix(target=[0, 1])
- static density_matrix(target=None)
Registers this function into the circuit class.
- Parameters:
target (QubitSetInput | None) – The target qubits of the reduced density matrix. Default is
None, and the full density matrix is returned.- Return type:
ResultType
- Returns:
ResultType – density matrix as a requested result type
Examples
>>> circ = Circuit().density_matrix(target=[0, 1])
- class Expectation(observable, target=None)
Bases:
ObservableResultTypeExpectation of the specified target qubit set and observable as the requested result type.
If no targets are specified, the observable must operate only on 1 qubit and it is applied to all qubits in parallel. Otherwise, the number of specified targets must be equivalent to the number of qubits the observable can be applied to.
For
shots>0, this is calculated by measurements. Forshots=0, this is supported only by simulators and represents the exact result.See
braket.circuits.observablesmodule for all of the supported observables.- Parameters:
observable (Observable)
target (QubitSetInput | None)
Inits an
Expectation.- Parameters:
observable (Observable) – the observable for the result type
target (QubitSetInput | None) – Target qubits that the result type is requested for. If not provided, the observable’s target will be used instead. If neither exist, then it is applied to all qubits in parallel; in this case the observable must operate only on 1 qubit. Default:
None.
Examples
>>> result_types.Expectation(observable=observables.Z(0)) >>> result_types.Expectation(observable=observables.Z(), target=0)
>>> tensor_product = observables.Y(0) @ observables.Z(1) >>> result_types.Expectation(observable=tensor_product)
- static expectation(observable, target=None)
Registers this function into the circuit class.
- Parameters:
observable (Observable) – the observable for the result type
target (QubitSetInput | None) – Target qubits that the result type is requested for. Default is
None, which means the observable must operate only on 1 qubit and it is applied to all qubits in parallel.
- Return type:
ResultType
- Returns:
ResultType – expectation as a requested result type
Examples
>>> circ = Circuit().expectation(observable=observables.Z(0))
- class Probability(target=None)
Bases:
ResultTypeProbability in the computational basis as the requested result type.
It can be the probability of all states if no targets are specified, or the marginal probability of a restricted set of states if only a subset of all qubits are specified as targets.
For
shots>0, this is calculated by measurements. Forshots=0, this is supported only on simulators and represents the exact result.- Parameters:
target (QubitSetInput | None)
Inits a
Probability.- Parameters:
target (QubitSetInput | None) – The target qubits that the result type is requested for. Default is
None, which means all qubits for the circuit.
Examples
>>> result_types.Probability(target=[0, 1])
- static probability(target=None)
Registers this function into the circuit class.
- Parameters:
target (QubitSetInput | None) – The target qubits that the result type is requested for. Default is
None, which means all qubits for the circuit.- Return type:
ResultType
- Returns:
ResultType – probability as a requested result type
Examples
>>> circ = Circuit().probability(target=[0, 1])
- class Sample(observable, target=None)
Bases:
ObservableResultTypeSample of specified target qubit set and observable as the requested result type.
If no targets are specified, the observable must operate only on 1 qubit and it is applied to all qubits in parallel. Otherwise, the number of specified targets must equal the number of qubits the observable can be applied to.
This is only available for
shots>0.See
braket.circuits.observablesmodule for all of the supported observables.- Parameters:
observable (Observable)
target (QubitSetInput | None)
Inits a
Sample.- Parameters:
observable (Observable) – the observable for the result type
target (QubitSetInput | None) – Target qubits that the result type is requested for. If not provided, the observable’s target will be used instead. If neither exist, then it is applied to all qubits in parallel; in this case the observable must operate only on 1 qubit. Default:
None.
Examples
>>> result_types.Sample(observable=observables.Z(0)) >>> result_types.Sample(observable=observables.Z(), target=0)
>>> tensor_product = observables.Y(0) @ observables.Z(1) >>> result_types.Sample(observable=tensor_product)
- static sample(observable, target=None)
Registers this function into the circuit class.
- Parameters:
observable (Observable) – the observable for the result type
target (QubitSetInput | None) – Target qubits that the result type is requested for. Default is
None, which means the observable must operate only on 1 qubit and it is applied to all qubits in parallel.
- Return type:
ResultType
- Returns:
ResultType – sample as a requested result type
Examples
>>> circ = Circuit().sample(observable=observables.Z(0))
- class StateVector
Bases:
ResultTypeThe full state vector as a requested result type. This is available on simulators only when
shots=0.Initializes a
ResultType.- Parameters:
ascii_symbols (list[str]) – ASCII string symbols for the result type. This is used when printing a diagram of circuits.
- Raises:
ValueError –
ascii_symbolsisNone
- static state_vector()
Registers this function into the circuit class.
- Return type:
- Returns:
ResultType – state vector as a requested result type
Examples
>>> circ = Circuit().state_vector()
- class Variance(observable, target=None)
Bases:
ObservableResultTypeVariance of specified target qubit set and observable as the requested result type.
If no targets are specified, the observable must operate only on 1 qubit and it is applied to all qubits in parallel. Otherwise, the number of targets specified must equal the number of qubits that the observable can be applied to.
For
shots>0, this is calculated by measurements. Forshots=0, this is supported only by simulators and represents the exact result.See
braket.circuits.observablesmodule for all of the supported observables.- Parameters:
observable (Observable)
target (QubitSetInput | None)
Inits a
Variance.- Parameters:
observable (Observable) – the observable for the result type
target (QubitSetInput | None) – Target qubits that the result type is requested for. If not provided, the observable’s target will be used instead. If neither exist, then it is applied to all qubits in parallel; in this case the observable must operate only on 1 qubit. Default:
None.
- Raises:
ValueError – If the observable’s qubit count does not equal the number of target qubits, or if
target=Noneand the observable’s qubit count is not 1.
Examples
>>> result_types.Variance(observable=observables.Z(0)) >>> result_types.Variance(observable=observables.Z(), target=0)
>>> tensor_product = observables.Y(0) @ observables.Z(1) >>> result_types.Variance(observable=tensor_product)
- static variance(observable, target=None)
Registers this function into the circuit class.
- Parameters:
observable (Observable) – the observable for the result type
target (QubitSetInput | None) – Target qubits that the result type is requested for. Default is
None, which means the observable must only operate on 1 qubit and it will be applied to all qubits in parallel
- Return type:
ResultType
- Returns:
ResultType – variance as a requested result type
Examples
>>> circ = Circuit().variance(observable=observables.Z(0))
- class braket.circuits.result_type.ObservableResultType(ascii_symbols, observable, target=None)[source]
Bases:
ResultTypeResult types with observables and targets. If no targets are specified, the observable must only operate on 1 qubit and it will be applied to all qubits in parallel. Otherwise, the number of specified targets must be equivalent to the number of qubits the observable can be applied to.
See
braket.circuits.observablesmodule for all of the supported observables.- Parameters:
ascii_symbols (list[str])
observable (Observable)
target (QubitSetInput | None)
Initializes an
ObservableResultType.- Parameters:
ascii_symbols (list[str]) – ASCII string symbols for the result type. This is used when printing a diagram of circuits.
observable (Observable) – the observable for the result type
target (QubitSetInput | None) – Target qubits that the result type is requested for. Default is
None, which means the observable must only operate on 1 qubit and it will be applied to all qubits in parallel
- Raises:
ValueError – if target=None and the observable’s qubit count is not 1. Or, if
target!=Noneand the observable’s qubit count and the number of target qubits are not equal. Or, iftarget!=Noneand the observable’s qubit count and the number ofascii_symbolsare not equal.
- property observable: Observable
- class braket.circuits.result_type.ObservableParameterResultType(ascii_symbols, observable, target=None, parameters=None)[source]
Bases:
ObservableResultTypeResult types with observables, targets and parameters. If no targets are specified, the observable must only operate on 1 qubit and it will be applied to all qubits in parallel. Otherwise, the number of specified targets must be equivalent to the number of qubits the observable can be applied to. If no parameters are specified, observable will be applied to all the free parameters.
See
braket.circuits.observablesmodule for all of the supported observables.- Parameters:
ascii_symbols (list[str])
observable (Observable)
target (QubitSetInput | None)
parameters (list[str | FreeParameter] | None)
Initializes an
ObservableResultType.- Parameters:
ascii_symbols (list[str]) – ASCII string symbols for the result type. This is used when printing a diagram of circuits.
observable (Observable) – the observable for the result type
target (QubitSetInput | None) – Target qubits that the result type is requested for. Default is
None, which means the observable must only operate on 1 qubit and it will be applied to all qubits in parallelparameters (list[str | FreeParameter] | None)
- Raises:
ValueError – if target=None and the observable’s qubit count is not 1. Or, if
target!=Noneand the observable’s qubit count and the number of target qubits are not equal. Or, iftarget!=Noneand the observable’s qubit count and the number ofascii_symbolsare not equal.
- property parameters: list[str]