mindquantum.ops

Fermion operator and qubit operator.

class mindquantum.ops.FermionOperator(term=None, coefficient=1.0)[source]

The Fermion Operator such as FermionOperator(’ 4^ 3 9 3^ ‘) are used to represent \(a_4^\dagger a_3 a_9 a_3^\dagger\). These are the Basic Operators to describe a fermionic system, such as a Molecular system. The FermionOperator are follows the anti-commutation relationship.

Parameters
  • terms (str) – The input term of fermion operator. Default: None.

  • coefficient (Union[numbers.Number, str, ParameterResolver]) – The coefficient for the corresponding single operators Default: 1.0.

Examples

>>> from mindquantum.ops import FermionOperator
>>> a_p_dagger = FermionOperator('1^')
>>> a_p_dagger
1.0 [1^]
>>> a_q = FermionOperator('0')
>>> a_q
1.0 [0]
>>> zero= FermionOperator()
>>> 0
>>> identity= FermionOperator('')
>>> 1.0 []
>>> # check with coefficient
>>> para_op = FermionOperator('0 1^', 'x')
x [0 1^]
>>> para_dt = {'x':2}
>>> op = para_op.subs(para_dt)
>>> op
2 [0 1^]
property imag

Convert the coeff to its imag part.

Returns

FermionOperator, the imag part of this fermion operator.

Examples

>>> from mindquantum.ops import FermionOperator
>>> f = FermionOperator('0', 1 + 2j) + FermionOperator('0^', 'a')
>>> f.imag.compress()
2.0 [0]
normal_ordered()[source]

Return the normal ordered form of the Fermion Operator.

Returns

FermionOperator, the normal ordered FermionOperator.

Exmples:
>>> from mindquantum.ops import FermionOperator
>>> origin = FermionOperator('0 1^')
>>> origin
1.0 [0 1^]
>>> origin.normal_ordered()
-1.0 [1^ 0]
property real

Convert the coeff to its real part.

Returns

FermionOperator, the real part of this fermion operator.

Examples

>>> from mindquantum.ops import FermionOperator
>>> f = FermionOperator('0', 1 + 2j) + FermionOperator('0^', 'a')
>>> f.real.compress()
1.0 [0] +
a [0^]
class mindquantum.ops.InteractionOperator(constant, one_body_tensor, two_body_tensor)[source]

Class to store ‘interaction opeartors’ which are used to configure a ferinonic molecular Hamiltonian.

The Hamiltonian including one-body and two-body terms which conserve spin and parity. In this module, the stored coefficient could be represented the molecualr Hamiltonians througth the FermionOperator class.

Note

The operators stored in this class has the form:

\[C + \sum_{p, q} h_{[p, q]} a^\dagger_p a_q + \sum_{p, q, r, s} h_{[p, q, r, s]} a^\dagger_p a^\dagger_q a_r a_s.\]

Where \(C\) is a constant.

Parameters
  • constant (numbers.Number) – A constant term in the operator given as a float. For instance, the nuclear repulsion energy.

  • one_body_tensor (numpy.ndarray) – The coefficients of the one-body terms (h[p, q]). This is an \(n_\text{qubits}\times n_\text{qubits}\) numpy array of floats. By default we store the numpy array with keys: \(a^\dagger_p a_q\) (1,0).

  • two_body_tensor (numpy.ndarray) – The coefficients of the two-body terms (h[p, q, r, s]). This is an \(n_\text{qubits}\times n_\text{qubits}\times n_\text{qubits}\times n_\text{qubits}\) numpy array of floats.By default we store the numpy array with keys: \(a^\dagger_p a^\dagger_q a_r a_s\) (1, 1, 0, 0).

unique_iter(complex_valued=False)[source]

Iterate all terms that are not in the same symmetry group.

Four point symmetry:
  1. pq = qp.

  2. pqrs = srqp = qpsr = rspq.

Eight point symmetry(when complex_valued is False):
  1. pq = qp.

  2. pqrs = rqps = psrq = srqp = qpsr = rspq = spqr = qrsp.

Parameters

complex_valued (bool) – Whether the operator has complex coefficients. Default: False.

class mindquantum.ops.PolynomialTensor(n_body_tensors=None)[source]

Class to store the coefficient of the fermionic ladder operators in a tensor form. For instance, in a molecular Hamiltonian (degree 4 polynomial) which conserves particle number, there are only three kinds of terms, namely constant term, single excitation \(a^\dagger_p a_q\) and double excitation terms \(a^\dagger_p a^\dagger_q a_r a_s\), and their corresponding coefficients can be stored in an scalar, \(n_\text{qubits}\times n_\text{qubits}\) matrix and \(n_\text{qubits}\times n_\text{qubits} n_\text{qubits}\times n_\text{qubits}\) matrix. Note that each tensor must have an even number of dimensions due to the parity conservation. Much of the functionality of this class is similar to that of FermionOperator.

Parameters

n_body_tensors (dict) – A dictionary storing the tensors describing n-body interactions. The keys are tuples that indicate the type of tensor. For instance, n_body_tensors[()] would return a constant, while a n_body_tensors[(1, 0)] would be an \(n_\text{qubits}\times n_\text{qubits}\) numpy array, and n_body_tensors[(1,1,0,0)] would return a \(n_\text{qubits}\times n_\text{qubits} n_\text{qubits}\times n_\text{qubits}\) numpy array and those constant and array represent the coefficients of terms of the form identity, \(a^\dagger_p a_q\), \(a^\dagger_p a^\dagger_q a_r a_s\), respectively. Default: None.

Note

Here ‘1’ represents \(a^\dagger\), while ‘0’ represent \(a\).

Examples

>>> import numpy as np
>>> from mindquantum.ops import PolynomialTensor
>>> constant = 1
>>> one_body_term = np.array([[1,0],[0,1]])
>>> two_body_term = two_body_term = np.array([[[[1,0],[0,1]],[[1,0],[0,1]]],[[[1,0],[0,1]],[[1,0],[0,1]]]])
>>> n_body_tensors = {(): 1, (1,0): one_body_term,(1,1,0,0):two_body_term}
>>> poly_op = PolynomialTensor(n_body_tensors)
>>> poly_op
() 1
((0, 1), (0, 0)) 1
((1, 1), (1, 0)) 1
((0, 1), (0, 1), (0, 0), (0, 0)) 1
((0, 1), (0, 1), (1, 0), (1, 0)) 1
((0, 1), (1, 1), (0, 0), (0, 0)) 1
((0, 1), (1, 1), (1, 0), (1, 0)) 1
((1, 1), (0, 1), (0, 0), (0, 0)) 1
((1, 1), (0, 1), (1, 0), (1, 0)) 1
((1, 1), (1, 1), (0, 0), (0, 0)) 1
((1, 1), (1, 1), (1, 0), (1, 0)) 1
>>> # get the constant
>>> poly_op.constant
1
>>> # set the constant
>>> poly_op.constant = 2
>>> poly_op.constant
2
>>> poly_op.n_qubits
2
>>> poly_op.one_body_tensor
array([[1, 0],
       [0, 1]])
>>> poly_op.two_body_tensor
array([[[[1, 0],
         [0, 1]],
        [[1, 0],
         [0, 1]]],
       [[[1, 0],
         [0, 1]],
         [[1, 0],
          [0, 1]]]])
property constant

get the value of the identity term

property one_body_tensor

get the one-body term

property two_body_tensor

get the two body term

class mindquantum.ops.QubitExcitationOperator(term=None, coefficient=1.0)[source]

The Qubit Excitation Operator is defined as: \(Q^{\dagger}_{n} = \frac{1}{2} (X_{n} - iY_{n})\) and \(Q_{n} = \frac{1}{2} (X_{n} + iY_{n})\). Compared with Fermion excitation operators, Qubit excitation operators are some kind of “localized”, i.e., the Fermion excitation operator \(a^{\dagger}_{7} a_{0}\) involves qubit ranging from 0 to 7 under JW transformation, while Qubit excitation \(Q^{\dagger}_{7} Q_{0}\) will only affect the 0th and 7th qubits. In addition, double excitations described using Qubit excitation operators use much less CNOTs than the corresponding Fermion excitation operators.

Parameters
  • terms (str) – The input term of qubit excitation operator. Default: None.

  • coefficient (Union[numbers.Number, str, ParameterResolver]) – The coefficient for the corresponding single operators Default: 1.0.

Examples

>>> from mindquantum.hiqfermion.transforms import Transform
>>> from mindquantum.ops import QubitExcitationOperator
>>> from mindquantum.hiqfermion.transforms import Transform
>>> op = QubitExcitationOperator(((4, 1), (1, 0), (0, 0)), 2.5)
>>> op
2.5 [Q4^ Q1 Q0]
>>> op.fermion_operator
2.5 [4^ 1 0]
>>> op.to_qubit_operator()
0.3125 [X0 X1 X4] +
-0.3125j [X0 X1 Y4] +
0.3125j [X0 Y1 X4] +
(0.3125+0j) [X0 Y1 Y4] +
0.3125j [Y0 X1 X4] +
(0.3125+0j) [Y0 X1 Y4] +
(-0.3125+0j) [Y0 Y1 X4] +
0.3125j [Y0 Y1 Y4]
>>> Transform(op.fermion_operator).jordan_wigner()
(0.3125+0j) [X0 X1 Z2 Z3 X4] +
-0.3125j [X0 X1 Z2 Z3 Y4] +
0.3125j [X0 Y1 Z2 Z3 X4] +
(0.3125+0j) [X0 Y1 Z2 Z3 Y4] +
0.3125j [Y0 X1 Z2 Z3 X4] +
(0.3125+0j) [Y0 X1 Z2 Z3 Y4] +
(-0.3125+0j) [Y0 Y1 Z2 Z3 X4] +
0.3125j [Y0 Y1 Z2 Z3 Y4]
property imag

Convert the coeff to its imag part.

Returns

QubitExcitationOperator, the image part of this qubit excitation operator.

Examples

>>> from mindquantum.ops import QubitExcitationOperator
>>> f = QubitExcitationOperator(((1, 0),), 1 + 2j)
>>> f += QubitExcitationOperator(((1, 1),), 'a')
>>> f.imag.compress()
2.0 [Q1^]
normal_ordered()[source]

Return the normal ordered form of the Qubit excitation operator.

Returns

QubitExcitationOperator, the normal ordered operator.

Examples

>>> from mindquantum.ops import QubitExcitationOperator
>>> op = QubitExcitationOperator("7 1^")
>>> op
1.0 [Q7 Q1^]
>>> op.normal_ordered()
1.0 [Q1^ Q7]

Note

Unlike Fermion excitation operators, Qubit excitation operators will not multiply -1 when the order is swapped.

property real

Convert the coeff to its imag part.

Returns

QubitExcitationOperator, the real part of this qubit excitation operator.

Examples

>>> from mindquantum.ops import QubitExcitationOperator
>>> f = QubitExcitationOperator(((1, 0),), 1 + 2j)
>>> f += QubitExcitationOperator(((1, 1),), 'a')
>>> f.real.compress()
1.0 [Q1] +
a [Q1^]
to_qubit_operator()[source]

Convert the Qubit excitation operator to the equivalent Qubit operator.

Returns

QubitOperator, The corresponding QubitOperator according to the definition of Qubit excitation operators.

Examples

>>> from mindquantum.ops import QubitExcitationOperator
>>> op = QubitExcitationOperator("7^ 1")
>>> op.to_qubit_operator()
0.25 [X1 X7] +
-0.25j [X1 Y7] +
0.25j [Y1 X7] +
(0.25+0j) [Y1 Y7]
class mindquantum.ops.QubitOperator(term=None, coefficient=1.0)[source]

A sum of terms acting on qubits, e.g., 0.5 * ‘X1 X5’ + 0.3 * ‘Z1 Z2’. A term is an operator acting on n qubits and can be represented as: coefficient * 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 mathematical notation a QubitOperator term is, for example, 0.5 * ‘X1 X5’, which means that a Pauli X operator acts on qubit 1 and 5, while the identity operator acts on all the rest qubits.

Note that a Hamiltonian composed of QubitOperators should be a hermitian operator, thus requires the coefficients of all terms must be real.

QubitOperator has the following attributes set as follows: operators = (‘X’, ‘Y’, ‘Z’), different_indices_commute = True.

Parameters
  • term (str) – The input term of qubit operator. Default: None.

  • coefficient (Union[numbers.Number, str, ParameterResolver]) – The coefficient of this qubit operator, could be a number or a variable represent by a string or a symbol or a parameter resolver. Default: 1.0.

Examples

>>> from mindquantum.ops import QubitOperator
>>> ham = ((QubitOperator('X0 Y3', 0.5)
            + 0.6 * QubitOperator('X0 Y3')))
# Equivalently
>>> ham2 = QubitOperator('X0 Y3', 0.5)
>>> ham2 += 0.6 * QubitOperator('X0 Y3')
>>> ham2
1.1 [X0 Y3]
>>> ham3 = QubitOperator('')
>>> ham3
1.0 []
>>> ham_para = QubitOperator('X0 Y3', 'x')
>>> ham_para
x [X0 Y3]
>>> ham_para.subs({'x':1.2})
1.2 [X0 Y3]
count_gates()[source]

Returns the gate number when treated in single Hamiltonian

Returns

int, number of the single qubit quantum gates.

property imag

Convert the coeff to its imag part.

Returns

QubitOperator, the imag part of this qubit operator.

Examples

>>> from mindquantum.ops import QubitOperator
>>> f = QubitOperator('X0', 1 + 2j) + QubitOperator('Y0', 'a')
>>> f.imag.compress()
2.0 [X0]
property real

Convert the coeff to its real part.

Returns

QubitOperator, the real part of this qubit operator.

Examples

>>> from mindquantum.ops import QubitOperator
>>> f = QubitOperator('X0', 1 + 2j) + QubitOperator('Y0', 'a')
>>> f.real.compress()
1.0 [X0] +
a [Y0]