QubitOperator
A sum of terms acting on qubits, e.g., 0.5 'X0 X5' + 0.3 'Z1 Z2'.
A term is an operator acting on n qubits and can be represented as:
coefficent * local_operator[0] x ... x local_operator[n-1]
where x is the tensor product. A local operator is a Pauli operator ('I', 'X', 'Y', or 'Z') which acts on one qubit. In math notation a term is, for example, 0.5 * 'X0 X5', which means that a Pauli X operator acts on qubit 0 and 5, while the identity operator acts on all other qubits.
A QubitOperator represents a sum of terms acting on qubits and overloads operations for easy manipulation of these objects by the user.
Note for a QubitOperator to be a Hamiltonian which is a hermitian operator, the coefficients of all terms must be real.
Example:
hamiltonian = 0.5 * QubitOperator('X0 X5') + 0.3 * QubitOperator('Z0')
Attributes:
terms (dict): **key**: A term represented by a tuple containing all
non-trivial local Pauli operators ('X', 'Y', or 'Z').
A non-trivial local Pauli operator is specified by a
tuple with the first element being an integer
indicating the qubit on which a non-trivial local
operator acts and the second element being a string,
either 'X', 'Y', or 'Z', indicating which non-trivial
Pauli operator acts on that qubit. Examples:
((1, 'X'),) or ((1, 'X'), (4,'Z')) or the identity ().
The tuples representing the non-trivial local terms
are sorted according to the qubit number they act on,
starting from 0.
*value**: Coefficient of this term as a (complex) float
Constructor Summary
Public Constructor | ||
public |
constructor(coefficient: number | Complex, term: Array<Array> | string) |
Member Summary
Public Members | ||
public |
terms: {} |
Method Summary
Public Methods | ||
public |
add(addend: Complex | number | QubitOperator): QubitOperator |
|
public |
Eliminates all terms with coefficients close to zero and removes imaginary parts of coefficients that are close to zero. |
|
public |
return copy of current qubit operator |
|
public |
div(divisor: *): * |
|
public |
iadd(addend: Complex | number | QubitOperator): QubitOperator in-Place add |
|
public |
idiv(divisor: Complex | number | QubitOperator): QubitOperator in-Place dived by divisor |
|
public |
imul(multiplier: Complex | number | QubitOperator): * In-place multiply (*=) terms with scalar or QubitOperator. |
|
public |
isClose(other: QubitOperator, realTolerance: number, absTolerance: number): boolean Returns true if other (QubitOperator) is close to this. |
|
public |
isub(subtrahend: Complex | number | QubitOperator): QubitOperator in-Place subtract |
|
public |
mul(multiplier: Complex | number | QubitOperator): QubitOperator Return self * multiplier for a scalar, or a QubitOperator. |
|
public |
return negative of current qubit operator |
|
public |
sub(subtrahend: *): * |
|
public |
string description of current qubit operator |
Public Constructors
public constructor(coefficient: number | Complex, term: Array<Array> | string) source
Params:
Name | Type | Attribute | Description |
coefficient | number | Complex | The coefficient of the first term of this QubitOperator. Default is 1.0. |
|
term | Array<Array> | string | (optional, empy array, a array of arrays, or a string): 1) Default is None which means there are no terms in the QubitOperator hence it is the "zero" Operator 2) An empty tuple means there are no non-trivial Pauli operators acting on the qubits hence only identities with a coefficient (which by default is 1.0). 3) A sorted tuple of tuples. The first element of each tuple is an integer indicating the qubit on which a non-trivial local operator acts, starting from zero. The second element of each tuple is a string, either 'X', 'Y' or 'Z', indicating which local operator acts on that qubit. 4) A string of the form 'X0 Z2 Y5', indicating an X on qubit 0, Z on qubit 2, and Y on qubit 5. The string should be sorted by the qubit number. '' is the identity. |
Throw:
QubitOperatorError |
Invalid operators provided to QubitOperator. |
Example:
ham = ((QubitOperator('X0 Y3', 0.5)
+ 0.6 * QubitOperator('X0 Y3')))
# Equivalently
ham2 = QubitOperator('X0 Y3', 0.5)
ham2 += 0.6 * QubitOperator('X0 Y3')
Note:
Adding terms to QubitOperator is faster using += (as this is done
by in-place addition). Specifying the coefficient in the __init__
is faster than by multiplying a QubitOperator with a scalar as
calls an out-of-place multiplication.
Public Members
public terms: {} source
Public Methods
public add(addend: Complex | number | QubitOperator): QubitOperator source
Params:
Name | Type | Attribute | Description |
addend | Complex | number | QubitOperator |
public compress(absTolerance: number) source
Eliminates all terms with coefficients close to zero and removes imaginary parts of coefficients that are close to zero.
Params:
Name | Type | Attribute | Description |
absTolerance | number | Absolute tolerance, must be at least 0.0 |
public iadd(addend: Complex | number | QubitOperator): QubitOperator source
in-Place add
Params:
Name | Type | Attribute | Description |
addend | Complex | number | QubitOperator |
public idiv(divisor: Complex | number | QubitOperator): QubitOperator source
in-Place dived by divisor
Params:
Name | Type | Attribute | Description |
divisor | Complex | number | QubitOperator |
public imul(multiplier: Complex | number | QubitOperator): * source
In-place multiply (*=) terms with scalar or QubitOperator.
Params:
Name | Type | Attribute | Description |
multiplier | Complex | number | QubitOperator |
Return:
* |
public isClose(other: QubitOperator, realTolerance: number, absTolerance: number): boolean source
Returns true if other (QubitOperator) is close to this.
Comparison is done for each term individually. Return true if the difference between each term in self and other is less than the relative tolerance w.r.t. either other or self (symmetric test) or if the difference is less than the absolute tolerance.
Params:
Name | Type | Attribute | Description |
other | QubitOperator | QubitOperator to compare against. |
|
realTolerance | number | Relative tolerance, must be greater than 0.0 |
|
absTolerance | number | Absolute tolerance, must be at least 0.0 |
public isub(subtrahend: Complex | number | QubitOperator): QubitOperator source
in-Place subtract
Params:
Name | Type | Attribute | Description |
subtrahend | Complex | number | QubitOperator |
public mul(multiplier: Complex | number | QubitOperator): QubitOperator source
Return self * multiplier for a scalar, or a QubitOperator.
Params:
Name | Type | Attribute | Description |
multiplier | Complex | number | QubitOperator | A scalar, or a QubitOperator. |
Throw:
Invalid type cannot be multiply with QubitOperator. |