# openfermion.ops.QubitOperator

A sum of terms acting on qubits, e.g., 0.5 * 'X0 X5' + 0.3 * 'Z1 Z2'.

Inherits From: SymbolicOperator

### Used in the notebooks

Used in the tutorials

A term is an operator acting on n qubits and can be represented as:

coefficient * 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.

.. code-block:: python

hamiltonian = 0.5 * QubitOperator('X0 X5') + 0.3 * QubitOperator('Z0')


QubitOperator is a subclass of SymbolicOperator. Importantly, it has attributes set as follows::

actions = ('X', 'Y', 'Z')
action_strings = ('X', 'Y', 'Z')
action_before_index = True
different_indices_commute = True


See the documentation of SymbolicOperator for more details.

.. code-block:: python

ham = ((QubitOperator('X0 Y3', 0.5)

    + 0.6 * QubitOperator('X0 Y3')))


# Equivalently

ham2 = QubitOperator('X0 Y3', 0.5) ham2 += 0.6 * QubitOperator('X0 Y3')

Adding QubitOperators is faster using += (as this is done by in-place addition). Specifying the coefficient during initialization is faster than multiplying a QubitOperator with a scalar.

action_before_index Whether action comes before index in string representations.
action_strings The string representations of the allowed actions.
actions The allowed actions.
constant The value of the constant term.
different_indices_commute Whether factors acting on different indices commute.

## Methods

### accumulate

View source

Sums over SymbolicOperators.

### compress

View source

Eliminates all terms with coefficients close to zero and removes small imaginary and real parts.

Args
abs_tol float

Absolute tolerance, must be at least 0.0

### get_operator_groups

View source

Gets a list of operators with a few terms.

Args
num_groups int

How many operators to get in the end.

Returns
operators [self.__class__]

A list of operators summing up to self.

### get_operators

View source

Gets a list of operators with a single term.

Returns
operators [self.__class__]

A generator of the operators in self.

### identity

View source

Returns: multiplicative_identity (SymbolicOperator): A symbolic operator u with the property that ux = xu = x for all operators x of the same class.

### induced_norm

View source

Compute the induced p-norm of the operator.

If we represent an operator as $$\sum_{j} w_j H_j$$ where $$w_j$$ are scalar coefficients then this norm is $$\left(\sum_{j} \| w_j \|^p \right)^{\frac{1}{p} }$$ where $$p$$ is the order of the induced norm

Args
order int

the order of the induced norm.

### isclose

View source

Check if other (SymbolicOperator) is close to self.

Comparison is done for each term individually. Return True if the difference between each term in self and other is less than EQ_TOLERANCE

Args
other SymbolicOperator

SymbolicOperator to compare against.

### many_body_order

View source

Compute the many-body order of a SymbolicOperator.

The many-body order of a SymbolicOperator is the maximum length of a term with nonzero coefficient.

Returns
int

### renormalize

View source

Fix the trace norm of an operator to 1

### zero

View source

Returns: additive_identity (SymbolicOperator): A symbolic operator o with the property that o+x = x+o = x for all operators x of the same class.

### __add__

View source

Returns
sum (SymbolicOperator)

### __div__

View source

For compatibility with Python 2.

### __eq__

View source

Approximate numerical equality (not true equality).

View source

### __mul__

View source

Return self * multiplier for a scalar, or a SymbolicOperator.

Args
multiplier A scalar, or a SymbolicOperator.

Returns
product (SymbolicOperator)

Raises
TypeError Invalid type cannot be multiply with SymbolicOperator.

### __ne__

View source

Return self!=value.

### __neg__

View source

Returns: negation (SymbolicOperator)

### __pow__

View source

Exponentiate the SymbolicOperator.

Args
exponent int

The exponent with which to raise the operator.

Returns
exponentiated (SymbolicOperator)

Raises
ValueError Can only raise SymbolicOperator to non-negative integer powers.

### __radd__

View source

Returns
sum (SymbolicOperator)

### __rmul__

View source

Return multiplier * self for a scalar.

We only define rmul for scalars because the left multiply exist for SymbolicOperator and left multiply is also queried as the default behavior.

Args
multiplier A scalar to multiply by.

Returns
product A new instance of SymbolicOperator.

Raises
TypeError Object of invalid type cannot multiply SymbolicOperator.

### __rsub__

View source

Args: subtrahend (SymbolicOperator): The operator to subtract.

Returns
difference (SymbolicOperator)

### __sub__

View source

Args: subtrahend (SymbolicOperator): The operator to subtract.

Returns
difference (SymbolicOperator)

### __truediv__

View source

Return self / divisor for a scalar.

Note
This is always floating point division.

Args
divisor A scalar to divide by.

Returns
A new instance of SymbolicOperator.

Raises
TypeError Cannot divide local operator by non-scalar type.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }]
[{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]