Home Reference Source ``import QubitOperator from 'projectq/src/ops/qubitoperator.js'``
public class

# 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 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 | string)

## Member Summary

 Public Members public terms: {}

## Method Summary

 Public Methods public add(addend: Complex | number | QubitOperator): QubitOperator public compress(absTolerance: number) 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 toString(): string string description of current qubit operator

## Public Constructors

### publicconstructor(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 | 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 Methods

#### Params:

 Name Type Attribute Description addend Complex | number | QubitOperator

### publiccompress(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

### publiccopy(): QubitOperator source

return copy of current qubit operator

### publicdiv(divisor: *): * source

#### Params:

 Name Type Attribute Description divisor *

 *

#### Params:

 Name Type Attribute Description addend Complex | number | QubitOperator

### publicidiv(divisor: Complex | number | QubitOperator): QubitOperator source

in-Place dived by divisor

#### Params:

 Name Type Attribute Description divisor Complex | number | QubitOperator

### publicimul(multiplier: Complex | number | QubitOperator): * source

In-place multiply (*=) terms with scalar or QubitOperator.

#### Params:

 Name Type Attribute Description multiplier Complex | number | QubitOperator

 *

### publicisClose(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

### publicisub(subtrahend: Complex | number | QubitOperator): QubitOperator source

in-Place subtract

#### Params:

 Name Type Attribute Description subtrahend Complex | number | QubitOperator

### publicmul(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:

 Error Invalid type cannot be multiply with QubitOperator.

### publicnegative(): QubitOperator source

return negative of current qubit operator

### publicsub(subtrahend: *): * source

#### Params:

 Name Type Attribute Description subtrahend *

 *

### publictoString(): string source

string description of current qubit operator