mindquantum.core.operators.FermionOperator

View Source On Gitee
class mindquantum.core.operators.FermionOperator(terms: Union[str, 'FermionOperator'] = None, coefficient: PRConvertible = 1.0, internal: bool = False)[source]

Definition of a Fermion Operator.

The Fermion Operator such as FermionOperator(‘9 4^ 3 3^’) are used to represent \(a_9 a_4^\dagger a_3 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

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> a_p_dagger = FermionOperator('1^')
>>> a_p_dagger
1 [1^]
>>> a_q = FermionOperator('0')
>>> a_q
1 [0]
>>> zero = FermionOperator()
>>> zero
0
>>> identity= FermionOperator('')
>>> identity
1 []
>>> para_op = FermionOperator('0 1^', 'x')
>>> para_op
-x [1^ 0]
>>> para_dt = {'x':2}
>>> op = para_op.subs(para_dt)
>>> op
-2 [1^ 0]
astype(dtype)[source]

Convert to different data type.

Note

Converting a complex type FermionOperator to real type will ignore the image part of coefficient.

Parameters

dtype (mindquantum.dtype) – new data type of fermion operator.

Returns

FermionOperator, new fermion operator with given data type.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> import mindquantum as mq
>>> f = FermionOperator('0^', 2 + 3j)
>>> f.dtype
mindquantum.complex128
>>> f.astype(mq.float64)
2 [0^]
cast_complex()[source]

Cast a FermionOperator into its complex equivalent.

compress(abs_tol=EQ_TOLERANCE)[source]

Eliminate the very small fermion string that close to zero.

Parameters

abs_tol (float) – Absolute tolerance, must be at least 0.0. Default: EQ_TOLERANCE.

Returns

FermionOperator, the compressed operator.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> ham_compress = FermionOperator('0^ 1', 0.5) + FermionOperator('2^ 3', 1e-7)
>>> ham_compress
1/2 [0^ 1] +
1/10000000 [2^ 3]
>>> ham_compress.compress(1e-6)
1/2 [0^ 1]
>>> ham_para_compress =  FermionOperator('0^ 1', 0.5) + FermionOperator('2^ 3', 'X')
>>> ham_para_compress
1/2 [0^ 1] +
X [2^ 3]
>>> ham_para_compress.compress(1e-7)
1/2 [0^ 1] +
X [2^ 3]
property constant: mindquantum.core.parameterresolver.parameterresolver.ParameterResolver

Return the coefficient of the identity fermion string.

Returns

ParameterResolver, the coefficient of the identity fermion string.

count_qubits()[source]

Calculate the number of qubits on which operator acts before removing the unused qubit.

Returns

int, the qubits number before remove unused qubit.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> a = FermionOperator("0^ 3")
>>> a.count_qubits()
4
property dtype

Get the data type of FermionOperator.

dumps(indent: int = 4)[source]

Dump a FermionOperator into JSON(JavaScript Object Notation).

Parameters

indent (int) – Then JSON array elements and object members will be pretty-printed with that indent level. Default: 4.

Returns

JSON (str), the JSON strings of this FermionOperator

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> f = FermionOperator('0', 1 + 2j) + FermionOperator('0^', 'a')
>>> len(f.dumps())
581
static from_openfermion(of_ops)[source]

Convert openfermion fermion operator to mindquantum format.

Parameters

of_ops (openfermion.FermionOperator) – fermion operator from openfermion.

Returns

FermionOperator, fermion operator from mindquantum.

get_coeff(term)[source]

Get coefficient of given term.

Parameters

term (List[Tuple[int, Union[int, str]]]) – the term you want get coefficient.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> f = FermionOperator('0^ 1', 1.2)
>>> f.get_coeff([(1, ''), (0, '^')])
ParameterResolver(dtype: float64, const: -1.200000)
>>> f.get_coeff([(1, 0), (0, 1)])
ParameterResolver(dtype: float64, const: -1.200000)
hermitian()[source]

Get the hermitian of a FermionOperator.

Returns

FermionOperator, The hermitian of this FermionOperator.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> a = FermionOperator("0^ 1", {"a": 1 + 2j})
>>> a.hermitian()
(-1 + 2j)*a [1 0^]
property imag: mindquantum.core.operators.fermion_operator.FermionOperator

Convert the coefficient to its imag part.

Returns

Fermion, the imag part of this FermionOperator.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> f = FermionOperator('0', 1 + 2j) + FermionOperator('0^', 'a')
>>> f.imag
2 [0]
property is_complex: bool

Return whether the FermionOperator instance is currently using complex coefficients.

property is_singlet: bool

To verify whether this operator has only one term.

Returns

bool, whether this operator has only one term.

static loads(strs: str)[source]

Load JSON(JavaScript Object Notation) into a FermionOperator.

Parameters

strs (str) – The dumped fermion operator string.

Returns

FermionOperator, the FermionOperator loaded from JSON-formatted strings

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> f = FermionOperator('0', 1 + 2j) + FermionOperator('0^', 'a')
>>> obj = FermionOperator.loads(f.dumps())
>>> obj == f
True
matrix(n_qubits: int = None, pr=None)[source]

Convert this fermion operator to csr_matrix under jordan_wigner mapping.

Parameters
normal_ordered()[source]

Return the normal ordered form of the Fermion Operator.

Returns

FermionOperator, the normal ordered FermionOperator.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> origin = FermionOperator('0 1^')
>>> origin
1.0 [0 1^]
>>> origin.normal_ordered()
-1.0 [1^ 0]
property parameterized: bool

Check whether this FermionOperator is parameterized.

property params_name

Get all parameters of this operator.

property real: mindquantum.core.operators.fermion_operator.FermionOperator

Convert the coefficient to its real part.

Returns

FermionOperator, the real part of this FermionOperator.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> f = FermionOperator('0', 1 + 2j) + FermionOperator('0^', 'a')
>>> f.real
1 [0] +
a [0^]
relabel(logic_qubits: List[int])[source]

Relabel the qubit according to the given logic qubits order.

Parameters

logic_qubits (List[int]) – The label of logic qubits. For example, if logic_qubits is [2, 0, 1], original qubit 0 will label as 2.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> o = FermionOperator('3^ 2 1 0')
>>> o
1 [3^ 2 1 0]
>>> o.relabel([1, 3, 0, 2])
-1 [3 2^ 1 0]
singlet()[source]

Split the single string operator into every word.

Returns

List[FermionOperator], The split word of the string.

Raises

RuntimeError – if the size of terms is not equal to 1.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> ops = FermionOperator("1^ 2", 1)
>>> print(ops.singlet())
[1 [2], 1 [1^]]
singlet_coeff()[source]

Get the coefficient of this operator, if the operator has only one term.

Returns

ParameterResolver, the coefficient of this single string operator.

Raises

RuntimeError – if the size of terms is not equal to 1.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> ops = FermionOperator("1^ 2", "a")
>>> print(ops)
-a [2 1^]
>>> print(ops.singlet_coeff())
-a
property size: int

Return the number of terms of this FermionOperator.

split()[source]

Split the coefficient and the operator.

Returns

List[List[ParameterResolver, FermionOperator]], the split result.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> a = FermionOperator('0', 'a') + FermionOperator('1^', 1.2)
>>> for i, j in a.split():
...     print(i, j)
a, 1 [0]
1.2, 1 [1^]
subs(params_value: PRConvertible)[source]

Replace the symbolical representation with the corresponding value.

Parameters

params_value (Union[Dict[str, numbers.Number], ParameterResolver]) – the value of variable in coefficient.

Examples

>>> from mindquantum.core.operators import FermionOperator
>>> from mindquantum.core.parameterresolver import ParameterResolver
>>> f = FermionOperator('0^', ParameterResolver({'a': 2.0}, 3.0))
>>> f
2*a + 3 [0^]
>>> f.subs({'a': 1.5})
6 [0^]
property terms: Dict[Tuple[int, int], mindquantum.core.parameterresolver.parameterresolver.ParameterResolver]

Get the terms of a FermionOperator.

to_openfermion()[source]

Convert a FermionOperator openfermion format.