Observable
- class braket.circuits.observable.Observable(qubit_count, ascii_symbols, targets=None)[source]
Bases:
QuantumOperatorClass
Observableto represent a quantum observable.Objects of this type can be used as input to
ResultType.Sample,ResultType.Variance,ResultType.Expectationto specify the measurement basis.- Parameters:
qubit_count (int)
ascii_symbols (Sequence[str])
targets (QubitSetInput | None)
Initializes a
QuantumOperator.- Parameters:
qubit_count (int | None) – Number of qubits this quantum operator acts on. If all instances of the operator act on the same number of qubits, this argument should be
None, andfixed_qubit_countshould be implemented to return the qubit count; iffixed_qubit_countis implemented and an int is passed in, it must equalfixed_qubit_count, or instantiation will raise a ValueError. An int must be passed in if instances can have a varying number of qubits, in which casefixed_qubit_countshould not be implemented,ascii_symbols (Sequence[str]) – ASCII string symbols for the quantum operator. These are used when printing a diagram of circuits. Length must be the same as
qubit_count, and index ordering is expected to correlate with target ordering on the instruction. For instance, if CNOT instruction has the control qubit on the first index and target qubit on the second index. Then ASCII symbols would have [“C”, “X”] to correlate a symbol with that index.targets (QubitSetInput | None)
- Raises:
TypeError –
qubit_countis not an intValueError –
qubit_countis less than 1,ascii_symbolsareNone,fixed_qubit_countis implemented and and not equal toqubit_count, orlen(ascii_symbols) != qubit_count
- to_ir(target=None, ir_type=IRType.JAQCD, serialization_properties=None)[source]
Returns the IR representation for the observable
- Parameters:
target (QubitSetInput | None) – target qubit(s). Defaults to None.
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:
str | list[str | list[list[list[float]]]]
- Returns:
str | list[str | list[list[list[float]]]] – The IR representation for the observable.
- Raises:
ValueError – If the supplied
ir_typeis not supported, or if the supplied serialization properties don’t correspond to their_type.
- property targets: QubitSet | None
The target qubits of this observable
If
None, this is provided by the enclosing result type.- Type:
QubitSet | None
- property coefficient: int
coefficient value of the observable.
- Type:
int
- property basis_rotation_gates: tuple[Gate, ...]
The basis rotation gates for this observable.
- Type:
tuple[Gate, …]
- property euler_angles: dict[str, float]
A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- Type:
dict[str, float]
- static get_euler_angles(targets)[source]
Get the ZXZ Euler angle mapping of this observable for the given qubit.
- Parameters:
targets (QubitSetInput) – The target qubits; this is used for the parameter names in the output mapping.
- Return type:
dict[str, float]
- Returns:
dict[str, float] – A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- property eigenvalues: ndarray
The eigenvalues of this observable.
- Type:
np.ndarray
- eigenvalue(index)[source]
Returns the eigenvalue of this observable at the given index.
The eigenvalues are ordered by their corresponding computational basis state after diagonalization.
- Parameters:
index (int) – The index of the desired eigenvalue
- Return type:
float- Returns:
float – The
indexth eigenvalue of the observable.
- classmethod register_observable(observable)[source]
Register an observable implementation by adding it into the
Observableclass.- Parameters:
observable (Observable) – Observable class to register.
- Return type:
None
- class H(target=None)
Bases:
StandardObservableHadamard operation as an observable.
- Parameters:
target (QubitInput | None)
Initializes Hadamard observable.
- Parameters:
target (QubitInput | None) – The target qubit to measure the observable on. If not provided, this needs to be provided from the enclosing result type. Default:
None.
Examples
>>> observables.H(0) >>> observables.H()
- property basis_rotation_gates: tuple[Gate, ...]
The basis rotation gates for this observable.
- Type:
tuple[Gate, …]
- to_matrix()
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Return type:
ndarray- Returns:
np.ndarray – A matrix representation of the quantum operator
- class Hermitian(matrix, display_name='Hermitian', targets=None)
Bases:
ObservableHermitian matrix as an observable.
- Parameters:
matrix (np.ndarray)
display_name (str)
targets (QubitSetInput | None)
Inits a
Hermitian.- Parameters:
matrix (np.ndarray) – Hermitian matrix that defines the observable.
display_name (str) – Name to use for an instance of this Hermitian matrix observable for circuit diagrams. Defaults to
Hermitian.targets (QubitSetInput | None) – The target qubits to measure the observable on. If not provided, this needs to be provided from the enclosing result type. Default:
None.
- Raises:
ValueError – If
matrixis not a two-dimensional square matrix, is not Hermitian, or has a dimension length that is either not a positive power of 2 or, if targets is supplied, doesn’t match the size of targets.
Examples
>>> observables.Hermitian(matrix=np.array([[0, 1], [1, 0]]), targets=[0]) >>> observables.Hermitian(matrix=np.array([[0, 1], [1, 0]]))
- property basis_rotation_gates: tuple[Gate, ...]
The basis rotation gates for this observable.
- Type:
tuple[Gate, …]
- eigenvalue(index)
Returns the eigenvalue of this observable at the given index.
The eigenvalues are ordered by their corresponding computational basis state after diagonalization.
- Parameters:
index (int) – The index of the desired eigenvalue
- Return type:
float- Returns:
float – The
indexth eigenvalue of the observable.
- property eigenvalues: ndarray
Returns the eigenvalues of this observable.
- Returns:
np.ndarray – The eigenvalues of this observable.
- to_matrix()
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Return type:
ndarray- Returns:
np.ndarray – A matrix representation of the quantum operator
- class I(target=None)
Bases:
ObservableIdentity operation as an observable.
- Parameters:
target (QubitInput | None)
Initializes Identity observable.
- Parameters:
target (QubitInput | None) – The target qubit to measure the observable on. If not provided, this needs to be provided from the enclosing result type. Default:
None.
Examples
>>> observables.I(0) >>> observables.I()
- property basis_rotation_gates: tuple[Gate, ...]
The basis rotation gates for this observable.
- Type:
tuple[Gate, …]
- eigenvalue(index)
Returns the eigenvalue of this observable at the given index.
The eigenvalues are ordered by their corresponding computational basis state after diagonalization.
- Parameters:
index (int) – The index of the desired eigenvalue
- Return type:
float- Returns:
float – The
indexth eigenvalue of the observable.
- property eigenvalues: ndarray
Returns the eigenvalues of this observable.
- Returns:
np.ndarray – The eigenvalues of this observable.
- property euler_angles: dict[str, float]
A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- Type:
dict[str, float]
- static get_euler_angles(targets)
Get the ZXZ Euler angle mapping of this observable for the given qubit.
- Parameters:
targets (QubitSetInput) – The target qubits; this is used for the parameter names in the output mapping.
- Return type:
dict[str, float]
- Returns:
dict[str, float] – A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- to_matrix()
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Return type:
ndarray- Returns:
np.ndarray – A matrix representation of the quantum operator
- class Sum(observables, display_name='Hamiltonian')
Bases:
ObservableSum of observables
- Parameters:
observables (
list[Observable])display_name (
str)
Inits a
Sum.- Parameters:
observables (list[Observable]) – List of observables for Sum
display_name (str) – Name to use for an instance of this Sum observable for circuit diagrams. Defaults to
Hamiltonian.
Examples
>>> t1 = -3 * Observable.Y(0) + 2 * Observable.X(0) Sum(X('qubit_count': 1), Y('qubit_count': 1)) >>> t1.summands (X('qubit_count': 1), Y('qubit_count': 1))
- property basis_rotation_gates: tuple[Gate, ...]
The basis rotation gates for this observable.
- Type:
tuple[Gate, …]
- eigenvalue(index)
Returns the eigenvalue of this observable at the given index.
The eigenvalues are ordered by their corresponding computational basis state after diagonalization.
- Parameters:
index (int) – The index of the desired eigenvalue
- Return type:
float- Returns:
float – The
indexth eigenvalue of the observable.
- property eigenvalues: ndarray
The eigenvalues of this observable.
- Type:
np.ndarray
- property summands: tuple[Observable, ...]
The observables that comprise this sum.
- Type:
tuple[Observable]
- to_matrix()
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Return type:
ndarray- Returns:
np.ndarray – A matrix representation of the quantum operator
- class TensorProduct(observables)
Bases:
ObservableTensor product of observables
- Parameters:
observables (
list[Observable])
Initializes a
TensorProduct.- Parameters:
observables (list[Observable]) – List of observables for tensor product
Examples
>>> t1 = Observable.Y(0) @ Observable.X(1) >>> t1.to_matrix() array([[0.+0.j, 0.+0.j, 0.-0.j, 0.-1.j], [0.+0.j, 0.+0.j, 0.-1.j, 0.-0.j], [0.+0.j, 0.+1.j, 0.+0.j, 0.+0.j], [0.+1.j, 0.+0.j, 0.+0.j, 0.+0.j]]) >>> t2 = Observable.Z(3) @ t1 >>> t2.factors (Z('qubit_count': 1), Y('qubit_count': 1), X('qubit_count': 1))
Note: You must provide the list of observables for the tensor product to be evaluated in the order that you want the tensor product to be calculated. For
TensorProduct(observables=[ob1, ob2, ob3]), the tensor product’s matrix is the result of the tensor product ofob1,ob2,ob3, ornp.kron(np.kron(ob1.to_matrix(), ob2.to_matrix()), ob3.to_matrix()).- property ascii_symbols: tuple[str, ...]
Returns the ascii symbols for the quantum operator.
- Type:
tuple[str, …]
- property basis_rotation_gates: tuple[Gate, ...]
Returns the basis rotation gates for this observable.
- Returns:
tuple[Gate, …] – The basis rotation gates for this observable.
- eigenvalue(index)
Returns the eigenvalue of this observable at the given index.
The eigenvalues are ordered by their corresponding computational basis state after diagonalization.
- Parameters:
index (int) – The index of the desired eigenvalue
- Return type:
float- Returns:
float – The
indexth eigenvalue of the observable.
- property eigenvalues: ndarray
Returns the eigenvalues of this observable.
- Returns:
np.ndarray – The eigenvalues of this observable.
- property euler_angles: dict[str, float]
A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- Type:
dict[str, float]
- property factors: tuple[Observable, ...]
The observables that comprise this tensor product.
- Type:
tuple[Observable]
- get_euler_angles(targets)
Get the ZXZ Euler angle mapping of this observable for the given qubit.
- Parameters:
targets (QubitSetInput) – The target qubits; this is used for the parameter names in the output mapping.
- Return type:
dict[str, float]
- Returns:
dict[str, float] – A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- to_matrix()
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Return type:
ndarray- Returns:
np.ndarray – A matrix representation of the quantum operator
- class X(target=None)
Bases:
StandardObservablePauli-X operation as an observable.
- Parameters:
target (QubitInput | None)
Initializes Pauli-X observable.
- Parameters:
target (QubitInput | None) – The target qubit to measure the observable on. If not provided, this needs to be provided from the enclosing result type. Default:
None.
Examples
>>> observables.X(0) >>> observables.X()
- property basis_rotation_gates: tuple[Gate, ...]
The basis rotation gates for this observable.
- Type:
tuple[Gate, …]
- property euler_angles: dict[str, float]
A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- Type:
dict[str, float]
- static get_euler_angles(targets)
Get the ZXZ Euler angle mapping of this observable for the given qubit.
- Parameters:
targets (QubitSetInput) – The target qubits; this is used for the parameter names in the output mapping.
- Return type:
dict[str, float]
- Returns:
dict[str, float] – A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- to_matrix()
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Return type:
ndarray- Returns:
np.ndarray – A matrix representation of the quantum operator
- class Y(target=None)
Bases:
StandardObservablePauli-Y operation as an observable.
- Parameters:
target (QubitInput | None)
Initializes Pauli-Y observable.
- Parameters:
target (QubitInput | None) – The target qubit to measure the observable on. If not provided, this needs to be provided from the enclosing result type. Default:
None.
Examples
>>> observables.Y(0) >>> observables.Y()
- property basis_rotation_gates: tuple[Gate, ...]
The basis rotation gates for this observable.
- Type:
tuple[Gate, …]
- property euler_angles: dict[str, float]
A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- Type:
dict[str, float]
- static get_euler_angles(targets)
Get the ZXZ Euler angle mapping of this observable for the given qubit.
- Parameters:
targets (QubitSetInput) – The target qubits; this is used for the parameter names in the output mapping.
- Return type:
dict[str, float]
- Returns:
dict[str, float] – A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- to_matrix()
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Return type:
ndarray- Returns:
np.ndarray – A matrix representation of the quantum operator
- class Z(target=None)
Bases:
StandardObservablePauli-Z operation as an observable.
- Parameters:
target (QubitInput | None)
Initializes Pauli-Z observable.
- Parameters:
target (QubitInput | None) – The target qubit to measure the observable on. If not provided, this needs to be provided from the enclosing result type. Default:
None.
Examples
>>> observables.Z(0) >>> observables.Z()
- property basis_rotation_gates: tuple[Gate, ...]
The basis rotation gates for this observable.
- Type:
tuple[Gate, …]
- property euler_angles: dict[str, float]
A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- Type:
dict[str, float]
- static get_euler_angles(targets)
Get the ZXZ Euler angle mapping of this observable for the given qubit.
- Parameters:
targets (QubitSetInput) – The target qubits; this is used for the parameter names in the output mapping.
- Return type:
dict[str, float]
- Returns:
dict[str, float] – A mapping of standardized free parameter name to ZXZ Euler angle value that diagonalizes this observable.
- to_matrix()
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Return type:
ndarray- Returns:
np.ndarray – A matrix representation of the quantum operator
- class braket.circuits.observable.StandardObservable(ascii_symbols, target=None)[source]
Bases:
ObservableClass
StandardObservableto represent a Pauli-like quantum observable with eigenvalues of (+1, -1).- Parameters:
ascii_symbols (Sequence[str])
target (QubitInput | None)
Initializes a
QuantumOperator.- Parameters:
qubit_count (int | None) – Number of qubits this quantum operator acts on. If all instances of the operator act on the same number of qubits, this argument should be
None, andfixed_qubit_countshould be implemented to return the qubit count; iffixed_qubit_countis implemented and an int is passed in, it must equalfixed_qubit_count, or instantiation will raise a ValueError. An int must be passed in if instances can have a varying number of qubits, in which casefixed_qubit_countshould not be implemented,ascii_symbols (Sequence[str]) – ASCII string symbols for the quantum operator. These are used when printing a diagram of circuits. Length must be the same as
qubit_count, and index ordering is expected to correlate with target ordering on the instruction. For instance, if CNOT instruction has the control qubit on the first index and target qubit on the second index. Then ASCII symbols would have [“C”, “X”] to correlate a symbol with that index.target (QubitInput | None)
- Raises:
TypeError –
qubit_countis not an intValueError –
qubit_countis less than 1,ascii_symbolsareNone,fixed_qubit_countis implemented and and not equal toqubit_count, orlen(ascii_symbols) != qubit_count
- property eigenvalues: ndarray
The eigenvalues of this observable.
- Type:
np.ndarray
- eigenvalue(index)[source]
Returns the eigenvalue of this observable at the given index.
The eigenvalues are ordered by their corresponding computational basis state after diagonalization.
- Parameters:
index (int) – The index of the desired eigenvalue
- Return type:
float- Returns:
float – The
indexth eigenvalue of the observable.
- property ascii_symbols: tuple[str, ...]
Returns the ascii symbols for the quantum operator.
- Type:
tuple[str, …]