mindquantum.core.operators.QubitOperator
- class mindquantum.core.operators.QubitOperator(terms: Union[str, 'QubitOperator'] = None, coefficient: PRConvertible = 1.0, internal: bool = False)[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 (Union[str, QubitOperator]) – The input term of qubit operator. Default:
None
.coefficient (Union[numbers.Number, str, Dict[str, numbers.Number], 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
.internal (bool) – Whether the first argument is internal c++ object of QubitOperator or not. Default:
False
.
Examples
>>> from mindquantum.core.operators import QubitOperator >>> ham = ((QubitOperator('X0 Y3', 0.5) ... + 0.6 * QubitOperator('X0 Y3'))) >>> ham2 = QubitOperator('X0 Y3', 0.5) >>> ham2 += 0.6 * QubitOperator('X0 Y3') >>> ham2 1.1 [Y3 X0] >>> ham3 = QubitOperator('') >>> ham3 1 [] >>> ham_para = QubitOperator('X0 Y3', 'x') >>> ham_para x [Y3 X0] >>> ham_para.subs({'x':1.2}) 1.2 [Y3 X0]
- astype(dtype)[source]
Convert to different data type.
Note
Converting a complex type QubitOperator to real type will ignore the image part of coefficient.
- Parameters
dtype (mindquantum.dtype) – new data type of fermion operator.
- Returns
QubitOperator, new fermion operator with given data type.
Examples
>>> from mindquantum.core.operators import QubitOperator >>> import mindquantum as mq >>> f = QubitOperator('X0', 2 + 3j) >>> f.dtype mindquantum.complex128 >>> f.astype(mq.float64) 2 [X0]
- compress(abs_tol=EQ_TOLERANCE)[source]
Eliminate the very small pauli string that close to zero.
- Parameters
abs_tol (float) – Absolute tolerance, must be at least 0.0. Default: EQ_TOLERANCE.
- Returns
QubitOperator, the compressed operator.
Examples
>>> from mindquantum.core.operators import QubitOperator >>> ham_compress = QubitOperator('X0 Y1', 0.5) + QubitOperator('Z1 X3', 1e-7) >>> ham_compress 1/2 [Y1 X0] + 1/10000000 [X3 Z1] >>> ham_compress.compress(1e-6) 1/2 [Y1 X0] >>> ham_para_compress = QubitOperator('X0 Y1', 0.5) + QubitOperator('Z5', 'X') >>> ham_para_compress 1/2 [Y1 X0] + X [Z5] >>> ham_para_compress.compress(1e-7) 1/2 [Y1 X0] + X [Z5]
- count_gates()[source]
Return the gate number when treated in single Hamiltonian.
- Returns
int, number of the single qubit quantum gates.
Examples
>>> from mindquantum.core.operators import QubitOperator >>> a = QubitOperator("X0 Y1") + QubitOperator("X2 Z3") >>> a.count_gates() 4
- 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 QubitOperator >>> a = QubitOperator("Z0 Y3") >>> a.count_qubits() 4
- property dtype
Get the data type of QubitOperator.
- dumps(indent: int = 4)[source]
Dump a QubitOperator 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 QubitOperator
Examples
>>> from mindquantum.core.operators import QubitOperator >>> f = QubitOperator('0', 1 + 2j) + QubitOperator('0^', 'a') >>> len(f.dumps()) 581
- static from_openfermion(of_ops)[source]
Convert qubit operator from openfermion to mindquantum format.
- Parameters
of_ops (openfermion.QubitOperator) – Qubit operator from openfermion.
- Returns
QubitOperator, qubit operator from mindquantum.
- get_coeff(term)[source]
Get coefficient of given term.
Examples
>>> from mindquantum.core.operators import QubitOperator >>> q = QubitOperator('X0 Y1', 1.2) >>> q.get_coeff([(1, 'Y'), (0, 'X')]) ParameterResolver(dtype: float64, const: 1.200000)
- hermitian()[source]
Get the hermitian of a QubitOperator.
- Returns
QubitOperator, the hermitian of this QubitOperator.
Examples
>>> from mindquantum.core.operators import QubitOperator >>> a = QubitOperator("X0 Y1", {"a": 1 + 2j}) >>> a.hermitian() (-1 + 2j)*a [1 0^]
- property imag
Convert the coefficient to its imag part.
- Returns
QubitOperator, the imag part of this qubit operator.
Examples
>>> from mindquantum.core.operators import QubitOperator >>> f = QubitOperator('X0', 1 + 2j) + QubitOperator('Y0', 'a') >>> f.imag.compress() 2 [X0]
- property is_complex: bool
Return whether the QubitOperator 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 QubitOperator.
- Parameters
strs (str) – The dumped fermion operator string.
- Returns
QubitOperator, the QubitOperator loaded from JSON-formatted strings.
Examples
>>> from mindquantum.core.operators import QubitOperator >>> f = QubitOperator('0', 1 + 2j) + QubitOperator('0^', 'a') >>> obj = QubitOperator.loads(f.dumps()) >>> obj == f True
- matrix(n_qubits: int = None, pr=None)[source]
Convert this qubit operator to csr_matrix.
- Parameters
n_qubits (int) – The total qubits of final matrix. If
None
, the value will be the maximum local qubit number. Default:None
.pr (ParameterResolver, dict, numpy.ndarray, list, numbers.Number) – The parameter resolver for parameterized QubitOperator. Default: None.
- property params_name
Get all parameters of this operator.
- property real
Convert the coefficient to its real part.
- Returns
QubitOperator, the real part of this qubit operator.
Examples
>>> from mindquantum.core.operators import QubitOperator >>> f = QubitOperator('X0', 1 + 2j) + QubitOperator('Y0', 'a') >>> f.real.compress() 1 [X0] + a [Y0]
- 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 QubitOperator >>> o = QubitOperator('Z0 Y1 X2 Z3') >>> o 1 [Z0 Y1 X2 Z3] >>> o.relabel([1, 3, 0, 2]) 1 [X0 Z1 Z2 Y3]
- singlet()[source]
Split the single string operator into every word.
- Returns
List[QubitOperator], The split word of the string.
- Raises
RuntimeError – if the size of terms is not equal to 1.
Examples
>>> from mindquantum.core.operators import QubitOperator >>> ops = QubitOperator("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 QubitOperator >>> ops = QubitOperator("X0 Y1", "a") >>> print(ops) -a [2 1^] >>> print(ops.singlet_coeff()) -a
- split()[source]
Split the coefficient and the operator.
- Returns
List[List[ParameterResolver, QubitOperator]], the split result.
Examples
>>> from mindquantum.core.operators import QubitOperator >>> a = QubitOperator('X0', 'a') + QubitOperator('Z1', 1.2) >>> for i, j in a.split(): ... print(f"{i}, {j}") a, 1 [X0] 1.2, 1 [Z1]
- 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 QubitOperator >>> from mindquantum.core.parameterresolver import ParameterResolver >>> q = QubitOperator('X0', ParameterResolver({'a': 2.0}, 3.0)) >>> q 2*a + 3 [X0] >>> q.subs({'a': 1.5}) 6 [X0]
- property terms: Dict[Tuple[int, str], mindquantum.core.parameterresolver.parameterresolver.ParameterResolver]
Get the terms of a QubitOperator.