braket.circuits.observable module
- class braket.circuits.observable.Observable(qubit_count: int, ascii_symbols: Sequence[str])[source]
Bases:
QuantumOperator
Class
Observable
to represent a quantum observable.Objects of this type can be used as input to
ResultType.Sample
,ResultType.Variance
,ResultType.Expectation
to specify the measurement basis.Initializes a
QuantumOperator
.- Parameters:
qubit_count (Optional[int]) – 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_count
should be implemented to return the qubit count; iffixed_qubit_count
is 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_count
should 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.
- Raises:
TypeError –
qubit_count
is not an intValueError –
qubit_count
is less than 1,ascii_symbols
areNone
,fixed_qubit_count
is implemented and and not equal toqubit_count
, orlen(ascii_symbols) != qubit_count
- to_ir(target: QubitSet | None = None, ir_type: IRType = IRType.JAQCD, serialization_properties: SerializationProperties | None = None) str | list[str | list[list[list[float]]]] [source]
Returns the IR representation for the observable
- Parameters:
target (QubitSet | 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.
- Returns:
Union[str, list[Union[str, list[list[list[float]]]]]] – The IR representation for the observable.
- Raises:
ValueError – If the supplied
ir_type
is not supported, or if the supplied serialization properties don’t correspond to their_type
.
- property coefficient: int
The coefficient of the observable.
- Returns:
int – coefficient value of the observable.
- property basis_rotation_gates: tuple[Gate, ...]
Returns the basis rotation gates for this observable.
- Returns:
tuple[Gate, …] – The basis rotation gates for this observable.
- property eigenvalues: ndarray
Returns the eigenvalues of this observable.
- Returns:
np.ndarray – The eigenvalues of this observable.
- eigenvalue(index: int) float [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
- Returns:
float – The
index
th eigenvalue of the observable.
- classmethod register_observable(observable: Observable) None [source]
Register an observable implementation by adding it into the
Observable
class.- Parameters:
observable (Observable) – Observable class to register.
- class H
Bases:
StandardObservable
Hadamard operation as an observable.
Examples: >>> Observable.H()
- property basis_rotation_gates: tuple[Gate, ...]
Returns the basis rotation gates for this observable.
- Returns:
tuple[Gate, …] – The basis rotation gates for this observable.
- to_matrix() ndarray
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Returns:
np.ndarray – A matrix representation of the quantum operator
- class Hermitian(matrix: ndarray, display_name: str = 'Hermitian')
Bases:
Observable
Hermitian matrix as an observable.
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
.
- Raises:
ValueError – If
matrix
is not a two-dimensional square matrix, or has a dimension length that is not a positive power of 2, or is not Hermitian.
Examples
>>> Observable.Hermitian(matrix=np.array([[0, 1],[1, 0]]))
- 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: int) float
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
- Returns:
float – The
index
th eigenvalue of the observable.
- property eigenvalues: ndarray
Returns the eigenvalues of this observable.
- Returns:
np.ndarray – The eigenvalues of this observable.
- to_matrix() ndarray
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Returns:
np.ndarray – A matrix representation of the quantum operator
- class I
Bases:
Observable
Identity operation as an observable.
Examples: >>> Observable.I()
- 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: int) float
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
- Returns:
float – The
index
th eigenvalue of the observable.
- property eigenvalues: ndarray
Returns the eigenvalues of this observable.
- Returns:
np.ndarray – The eigenvalues of this observable.
- to_matrix() ndarray
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Returns:
np.ndarray – A matrix representation of the quantum operator
- class Sum(observables: list[Observable], display_name: str = 'Hamiltonian')
Bases:
Observable
Sum of observables
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() + 2 * Observable.X() 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, ...]
Returns the basis rotation gates for this observable.
- Returns:
tuple[Gate, …] – The basis rotation gates for this observable.
- eigenvalue(index: int) float
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
- Returns:
float – The
index
th eigenvalue of the observable.
- property eigenvalues: ndarray
Returns the eigenvalues of this observable.
- Returns:
np.ndarray – The eigenvalues of this observable.
- property summands: tuple[Observable, ...]
The observables that comprise this sum.
- Type:
tuple[Observable]
- to_matrix() ndarray
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Returns:
np.ndarray – A matrix representation of the quantum operator
- class TensorProduct(observables: list[Observable])
Bases:
Observable
Tensor product of observables
Initializes a
TensorProduct
.- Parameters:
observables (list[Observable]) – List of observables for tensor product
Examples
>>> t1 = Observable.Y() @ Observable.X() >>> 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() @ 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: int) float
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
- Returns:
float – The
index
th eigenvalue of the observable.
- property eigenvalues: ndarray
Returns the eigenvalues of this observable.
- Returns:
np.ndarray – The eigenvalues of this observable.
- property factors: tuple[Observable, ...]
The observables that comprise this tensor product.
- Type:
tuple[Observable]
- to_matrix() ndarray
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Returns:
np.ndarray – A matrix representation of the quantum operator
- class X
Bases:
StandardObservable
Pauli-X operation as an observable.
Examples: >>> Observable.X()
- property basis_rotation_gates: tuple[Gate, ...]
Returns the basis rotation gates for this observable.
- Returns:
tuple[Gate, …] – The basis rotation gates for this observable.
- to_matrix() ndarray
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Returns:
np.ndarray – A matrix representation of the quantum operator
- class Y
Bases:
StandardObservable
Pauli-Y operation as an observable.
Examples: >>> Observable.Y()
- property basis_rotation_gates: tuple[Gate, ...]
Returns the basis rotation gates for this observable.
- Returns:
tuple[Gate, …] – The basis rotation gates for this observable.
- to_matrix() ndarray
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Returns:
np.ndarray – A matrix representation of the quantum operator
- class Z
Bases:
StandardObservable
Pauli-Z operation as an observable.
Examples: >>> Observable.Z()
- property basis_rotation_gates: tuple[Gate, ...]
Returns the basis rotation gates for this observable.
- Returns:
tuple[Gate, …] – The basis rotation gates for this observable.
- to_matrix() ndarray
Returns a matrix representation of the quantum operator.
- Parameters:
*args (Any) – Not Implemented.
**kwargs (Any) – Not Implemented.
- Raises:
NotImplementError – Not Implemented.
- Returns:
np.ndarray – A matrix representation of the quantum operator
- class braket.circuits.observable.StandardObservable(ascii_symbols: Sequence[str])[source]
Bases:
Observable
Class
StandardObservable
to represent a Pauli-like quantum observable with eigenvalues of (+1, -1).Initializes a
QuantumOperator
.- Parameters:
qubit_count (Optional[int]) – 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_count
should be implemented to return the qubit count; iffixed_qubit_count
is 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_count
should 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.
- Raises:
TypeError –
qubit_count
is not an intValueError –
qubit_count
is less than 1,ascii_symbols
areNone
,fixed_qubit_count
is implemented and and not equal toqubit_count
, orlen(ascii_symbols) != qubit_count
- property eigenvalues: ndarray
Returns the eigenvalues of this observable.
- Returns:
np.ndarray – The eigenvalues of this observable.
- eigenvalue(index: int) float [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
- Returns:
float – The
index
th eigenvalue of the observable.
- property ascii_symbols: tuple[str, ...]
Returns the ascii symbols for the quantum operator.
- Type:
tuple[str, …]