mindquantum

mindquantum.gate

Gate.

Gate provides different quantum gate.

class mindquantum.gate.BasicGate(name, isparameter=False)[source]

BasicGate is the base class of all gaets.

Parameters
  • name (str) – the name of this gate.

  • isparameter (bool) – whether this is a parameterized gate. Default: False.

abstract check_obj_qubits()[source]

Check obj qubit number

abstract define_projectq_gate()[source]

Define the corresponded projectq gate.

generate_description()[source]

Description generator.

abstract hermitian()[source]

Return the hermitian gate of this gate.

abstract matrix(*args)[source]

The matrix of the gate.

on(obj_qubits, ctrl_qubits=None)[source]

Define which qubit the gate act on and the control qubit.

Note

In this framework, the qubit that the gate act on is specified first, even for control gate, e.g. CNOT, the second arg is control qubits.

Parameters
  • obj_qubits (int, list[int]) – Specific which qubits the gate act on.

  • ctrl_qubits (int, list[int]) – Specific the control qbits. Default, None.

Returns

Gate, Return a new gate.

Examples

>>> from mindquantum.gate import X
>>> x = X.on(1)
>>> x.obj_qubits
[1]
>>> x.ctrl_qubits
[]
>>> x = X.on(2, [0, 1])
>>> x.ctrl_qubits
[0, 1]
class mindquantum.gate.IntrinsicOneParaGate(name, coeff=None)[source]

The parameterized gate that can be intrinsicly described by only one parameter.

Note

A parameterized gate can also be a non parameterized gate, if the parameter you send in is only a number.

Parameters
  • name (str) – the name of this parameterized gate.

  • coeff (Union[dict, ParameterResolver]) – the parameter of this gate. Default: Nnoe.

Examples

>>> from mindquantum.gate import RX
>>> rx1 = RX(1.2)
>>> rx1
RX(1.2)
>>> rx2 = RX({'a' : 0.5})
>>> rx2.coeff
{'a': 0.5}
>>> rx2.linearcombination(rx2.coeff,{'a' : 3})
1.5
check_obj_qubits()[source]

Check obj qubit number

diff_matrix(*paras_out, about_what=None)[source]

The differential form of this parameterized gate.

Parameters
  • paras_out (Union[dict, ParameterResolver]) – Parameters of this gate.

  • about_what (str) – Specific the differential is about which parameter. Default: None.

Returns

numpy.ndarray, Return the numpy array of the differential matrix.

Examples

>>> from mindquantum import RX
>>> rx = RX('a')
>>> np.round(rx.diff_matrix({'a' : 2}), 2)
array([[-0.42+0.j  ,  0.  -0.27j],
       [ 0.  -0.27j, -0.42+0.j  ]])
hermitian()[source]

Get the hermitian gate of this parameterized gate. Not inplace operation.

Note

We only set the coeff to -coeff.

Examples

>>> from mindquantum import RX
>>> rx = RX({'a': 1+2j})
>>> rx.hermitian()
RX(a*(-1.0 - 2.0*I))
matrix(*paras_out)[source]

The matrix of parameterized gate.

Note

If the parameterized gate convert to non parameterized gate, then you don’t need any parameters to get this matrix.

Parameters

paras_out (Union[dict, ParameterResolver]) – Parameters of this gate.

Returns

numpy.ndarray, Return the numpy array of the matrix.

Examples

>>> from mindquantum.gate import RX
>>> rx1 = RX(0)
>>> rx1.matrix()
array([[1.+0.j, 0.-0.j],
       [0.-0.j, 1.+0.j]])
>>> rx2 = RX({'a' : 1.2})
>>> np.round(rx2.matrix({'a': 2}), 2)
array([[0.36+0.j  , 0.  -0.93j],
       [0.  -0.93j, 0.36+0.j  ]])
class mindquantum.gate.NoneParameterGate(name)[source]

The basic class of gate that is not parametrized.

Parameters

name (str) – The name of the this gate.

check_obj_qubits()[source]

Check obj qubit number

hermitian()[source]

Get hermitian gate of this none parameterized gate.

matrix(*args)[source]

Get the matrix of this none parameterized gate.

class mindquantum.gate.ParameterGate(name, coeff=None)[source]

The basic class of gate that is parameterized.

Parameters
  • name (str) – the name of this gate.

  • coeff (Union[dict, ParameterResolver]) – the coefficients of this parameterized gate. Default: None.

static linearcombination(coeff_in, paras_out)[source]

Combine the parameters and coefficient.

Parameters
Returns

float, Multiply the values of the common keys of these two dicts.

no_grad()[source]

All parameters do not need grad. Inplace operation.

Returns

BaseGate, a parameterized gate with all parameters not need to update gradient.

no_grad_part(names)[source]

Set certain parameters that not need grad. Inplace operation.

Parameters

names (tuple[str]) – Parameters that not requires grad.

Returns

BaseGate, with some part of parameters not need to update gradient.

requires_grad()[source]

All parameters requires grad. Inplace operation.

Returns

BaseGate, a parameterized gate with all parameters need to

update gradient.

requires_grad_part(names)[source]

Set certain parameters that need grad. Inplace operation.

Parameters

names (tuple[str]) – Parameters that requires grad.

Returns

BaseGate, with some part of parameters need to update gradient.

class mindquantum.gate.Power(gate: mindquantum.gate.basic.NoneParameterGate, t=0.5)[source]

Power operator on a non parameterized gate.

Parameters

Examples

>>> from mindquantum import Power
>>> import numpy as np
>>> rx1 = RX(0.5)
>>> rx2 = RX(1)
>>> assert np.all(np.isclose(Power(rx2,0.5).matrix(), rx1.matrix()))
define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.UnivMathGate(name, mat)[source]

Universal math gate.

More usage, please see mindquantum.gate.XGate.

Parameters
  • name (str) – the name of this gate.

  • mat (np.ndarray) – the matrix value of this gate.

Examples

>>> from mindquantum.gate import UnivMathGate
>>> x_mat=np.array([[0,1],[1,0]])
>>> X_gate=UnivMathGate('X',x_mat)
>>> x1=X_gate.on(0,1)
>>> print(x1)
X(0 <-: 1)
define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.RX(coeff=None)[source]

Rotation gate around x-axis.

\[\begin{split}{\rm RX}=\begin{pmatrix}\cos(\theta/2)&-i\sin(\theta/2)\\ -i\sin(\theta/2)&\cos(\theta/2)\end{pmatrix}\end{split}\]

The rotation gate can be initialized in three different ways.

1. If you initialize it with a single number, then it will be a non parameterized gate with a certain rotation angle.

2. If you initialize it with a single str, then it will be a parameterized gate with only one parameter and the default coefficience is one.

3. If you initialize it with a dict, e.g. {‘a’:1,’b’:2}, this gate can have multiple parameters with certain coefficiences. In this case, it can be expressed as:

\[RX(a+2b)\]
Parameters

coeff (Union[int, float, str, dict]) – the parameters of parameterized gate, see above for detail explanation. Default: None.

Examples

>>> from mindquantum.gate import RX
>>> import numpy as np
>>> rx1 = RX(0.5)
>>> np.round(rx1.matrix(), 2)
array([[0.97+0.j  , 0.  -0.25j],
       [0.  -0.25j, 0.97+0.j  ]])
>>> rx2 = RX('a')
>>> np.round(rx2.matrix({'a':0.1}), 3)
array([[0.999+0.j  , 0.   -0.05j],
       [0.   -0.05j, 0.999+0.j  ]])
>>> rx3 = RX({'a' : 0.2, 'b': 0.5}).on(0, 2)
>>> print(rx3)
RX(a b|0 <-: 2)
>>> np.round(rx3.matrix({'a' : 1, 'b' : 2}), 2)
array([[0.83+0.j  , 0.  -0.56j],
       [0.  -0.56j, 0.83+0.j  ]])
>>> np.round(rx3.diff_matrix({'a' : 1, 'b' : 2}, about_what = 'a'), 2)
array([[-0.06+0.j  ,  0.  -0.08j],
       [ 0.  -0.08j, -0.06+0.j  ]])
>>> rx3.coeff
{'a': 0.2, 'b': 0.5}
define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.RY(coeff=None)[source]

Rotation gate around z-axis.

\[\begin{split}{\rm RY}=\begin{pmatrix}\cos(\theta/2)&-\sin(\theta/2)\\ \sin(\theta/2)&\cos(\theta/2)\end{pmatrix}\end{split}\]

More usage, please see mindquantum.gate.RX.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.RZ(coeff=None)[source]

Rotation gate around z-axis.

\[\begin{split}{\rm RZ}=\begin{pmatrix}\exp(-i\theta/2)&0\\ 0&\exp(i\theta/2)\end{pmatrix}\end{split}\]

More usage, please see mindquantum.gate.RX.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.PhaseShift(coeff=None)[source]

Phase shift gate.

\[\begin{split}{\rm PhaseShift}=\begin{pmatrix}1&0\\ 0&\exp(i\theta)\end{pmatrix}\end{split}\]

More usage, please see mindquantum.gate.RX.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.XX(coeff=None)[source]

Ising XX gate.

\[{\rm XX_\theta}=\cos(\theta)I\otimes I-i\sin(\theta)\sigma_x\otimes\sigma_x\]

More usage, please see mindquantum.gate.RX.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.YY(coeff=None)[source]

Ising YY gate.

\[{\rm YY_\theta}=\cos(\theta)I\otimes I-i\sin(\theta)\sigma_y\otimes\sigma_y\]

More usage, please see mindquantum.gate.RX.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.ZZ(coeff=None)[source]

Ising ZZ gate.

\[{\rm ZZ_\theta}=\cos(\theta)I\otimes I-i\sin(\theta)\sigma_Z\otimes\sigma_Z\]

More usage, please see mindquantum.gate.RX.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.IGate[source]

Identity gate with matrix as:

\[\begin{split}{\rm I}=\begin{pmatrix}1&0\\0&1\end{pmatrix}\end{split}\]

More usage, please see mindquantum.gate.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.XGate[source]

Pauli X gate with matrix as:

\[\begin{split}{\rm X}=\begin{pmatrix}0&1\\1&0\end{pmatrix}\end{split}\]

Note

For simplicity, you can do power operator on pauli gate (only works for pauli gate at this time). The rules is set below as:

\[X^\theta = RX(\theta\pi)\]

Examples

>>> from mindquantum.gate import X
>>> x1 = X.on(0)
>>> cnot = X.on(0, 1)
>>> print(x1)
X(0)
>>> print(cnot)
X(0 <-: 1)
>>> x1.matrix()
array([[0, 1],
       [1, 0]])
>>> x1**2
RX(6.283)
>>> (x1**'a').coeff
{'a': 3.141592653589793}
>>> (x1**{'a' : 2}).coeff
{'a': 6.283185307179586}
define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.YGate[source]

Pauli Y gate with matrix as:

\[\begin{split}{\rm Y}=\begin{pmatrix}0&-i\\i&0\end{pmatrix}\end{split}\]

More usage, please see mindquantum.gate.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.ZGate[source]

Pauli Z gate with matrix as:

\[\begin{split}{\rm Z}=\begin{pmatrix}1&0\\0&-1\end{pmatrix}\end{split}\]

More usage, please see mindquantum.gate.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.HGate[source]

Hadamard gate with matrix as:

\[\begin{split}{\rm H}=\frac{1}{\sqrt{2}}\begin{pmatrix}1&1\\1&-1\end{pmatrix}\end{split}\]

More usage, please see mindquantum.gate.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.SWAPGate[source]

SWAP gate that swap two different qubits.

More usage, please see mindquantum.gate.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.CNOTGate[source]

Control-X gate.

More usage, please see mindquantum.gate.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.gate.Hamiltonian(hamiltonian)[source]

A QubitOperator hamiltonian wrapper.

Parameters

hamiltonian (QubitOperator) – The pauli word qubit operator.

Examples

>>> from mindquantum.ops import QubitOperator
>>> from mindquantum import Hamiltonian
>>> ham = Hamiltonian(QubitOperator('Z0 Y1', 0.3))
>>> ham.mindspore_data()
{'hams_pauli_coeff': [0.3],
 'hams_pauli_word': [['Z', 'Y']],
 'hams_pauli_qubit': [[0, 1]]}
mindspore_data()[source]

Generate hamiltonian information for PQC operator.

class mindquantum.gate.Projector(proj)[source]

Projector operator.

For a projector shown as below:

\[\left|01\right>\left<01\right|\otimes I^2\]

The string format would be ‘01II’.

Note

The lower index qubit is at the right end of string format of bra and ket.

Parameters

proj (str) – The string format of the projector.

Examples

>>> from mindquantum.gate import Projector
>>> p = Projector('II010')
>>> p
I2 ⊗ ¦010⟩⟨010¦
mindspore_data()[source]

Generate projector information for PQC operator.

functional

The functional gates are the pre-instantiated quantum gates, which can be used directly as an instance of quantum gate.

functional

gates

mindquantum.gate.CNOT

mindquantum.gate.CNOTGate

mindquantum.gate.I

mindquantum.gate.IGate

mindquantum.gate.H

mindquantum.gate.HGate

mindquantum.gate.S

mindquantum.gate.PhaseShift (numpy.pi/2)

mindquantum.gate.SWAP

mindquantum.gate.SWAPGate

mindquantum.gate.X

mindquantum.gate.XGate

mindquantum.gate.Y

mindquantum.gate.YGate

mindquantum.gate.Z

mindquantum.gate.ZGate

mindquantum.circuit

Circuit.

Quantum circuit module.

class mindquantum.circuit.Circuit(gates=None)[source]

The quantum circuit module.

A quantum circuit contains one or more quantum gates, and can be evaluated in a quantum simulator. You can build a quantum circuit very easy by add a quantum gate or another circuit.

Parameters

gates (BasicGate, list[BasicGate]) – You can initialize the quantum circuit by a single quantum gate or a list of gates. gates: None.

Examples

>>> circuit1 = Circuit()
>>> circuit1 += RX('a').on(0)
>>> circuit1 *= 2
>>> print(circuit1)
RX(a|0)
RX(a|0)
>>> circuit2 = Circuit([X.on(0,1)])
>>> circuit3= circuit1 + circuit2
>>> assert len(circuit3) == 3
>>> circuit3.summary()
=======Circuit Summary=======
|Total number of gates  : 3.|
|Parameter gates        : 2.|
|with 1 parameters are  : a.|
|Number qubit of circuit: 2 |
=============================
append(gate)[source]

Append a gate.

apply_value(pr)[source]

Convert this circuit to a non parameterized circuit with parameter you input.

Parameters

pr (Union[dict, ParameterResolver]) – parameters you want to apply into this circuit.

Returns

Circuit, a non parameterized circuit.

Examples

>>> from mindquantum.gate import X, RX
>>> from mindquantum.circuit import Circuit
>>> circuit = Circuit()
>>> circuit += X.on(0)
>>> circuit += RX({'a': 2}).on(0)
>>> circuit = circuit.apply_value({'a': 1.5})
>>> circuit
X(0)
RX(3.0|0)
extend(gates)[source]

Extend a circuit.

h(obj_qubits, ctrl_qubits=None)[source]

Add a hadamard gate.

property hermitian

Get the hermitian of this quantum circuit.

Examples

>>> circ = Circuit(RX({'a': 0.2}).on(0))
>>> herm_circ = circ.hermitian
>>> herm_circ[0].coeff
{'a': -0.2}
insert(index, gates)[source]

Insert a quantum gate or quantum circuit in index.

Parameters
mindspore_data()[source]

Serialize the circuit. The result can be used by QNN operators.

no_grad()[source]

Set all parameterized gate in this quantum circuit not require grad.

property para_name

Get the parameter name of this circuit.

Returns

list, a list that contains the parameter name.

Examples

>>> from mindquantum.gate import RX
>>> from mindquantum.circuit import Circuit
>>> circuit = Circuit(RX({'a': 1, 'b': 2}).on(0))
>>> circuit.para_name
['a', 'b']
parameter_resolver()[source]

Get the parameter resolver of the whole circuit.

Note

This parameter resolver only tells you what are the parameters of this quantum circuit, and which part of parameters need grad, since the same parameter can be in different gate, and the coefficient can be different. The detail parameter resolver that shows the coefficient is in each gate of the circuit.

Returns

ParameterResolver, the parameter resolver of the whole circuit.

phase_shift(para, obj_qubits, ctrl_qubits=None)[source]

Add a Phase Shift gate.

requires_grad()[source]

Set all parameterized gates in this quantum circuit require grad.

rx(para, obj_qubits, ctrl_qubits=None)[source]

Add a RX gate.

ry(para, obj_qubits, ctrl_qubits=None)[source]

Add a RY gate.

rz(para, obj_qubits, ctrl_qubits=None)[source]

Add a RZ gate.

s(obj_qubits, ctrl_qubits=None)[source]

Add a S gate.

summary(show=True)[source]

Print the information about current circuit, including block number, gate number, non-parameterized gate number, parameterized gate number and the total parameters.

Parameters

show (bool) – whether to show the information. Default: True.

Examples

>>> from mindquantum import Circuit, RX, H
>>> circuit = Circuit([RX('a').on(1), H.on(1), RX('b').on(0)])
>>> circuit.summary()
========Circuit Summary========
|Total number of gates:3.     |
|Blocks               :1.     |
|Non parameter gates  :1.     |
|Parameter gates      :2.     |
|with 2 parameters are: b, a. |
===============================
swap(obj_qubits, ctrl_qubits=None)[source]

Add a SWAP gate.

x(obj_qubits, ctrl_qubits=None)[source]

Add a X gate.

xx(para, obj_qubits, ctrl_qubits=None)[source]

Add a XX gate.

y(obj_qubits, ctrl_qubits=None)[source]

Add a Y gate.

yy(para, obj_qubits, ctrl_qubits=None)[source]

Add a YY gate.

z(obj_qubits, ctrl_qubits=None)[source]

Add a Z gate.

zz(para, obj_qubits, ctrl_qubits=None)[source]

Add a ZZ gate.

class mindquantum.circuit.StateEvolution(circuit)[source]

Calculate the final state of a parameterized or non parameterized quantum circuit.

Parameters

circuit (Circuit) – The circuit that you want to do evolution.

Examples

>>> from mindquantum.circuit import StateEvolution
>>> from mindquantum.circuit import qft
>>> print(StateEvolution(qft([0, 1])).final_state(ket=True))
0.5¦00⟩
0.5¦01⟩
0.5¦10⟩
0.5¦11⟩
final_state(param=None, ket=False)[source]

Get the final state of the input quantum circuit.

Parameters
  • param (Union[Tensor, numpy.ndarray, ParameterResolver, dict]) – The parameter for the parameterized quantum circuit. If None, the quantum circuit should be a non parameterized quantum circuit. Default: None.

  • ket (bool) – Whether to print the final state in ket format. Default: False.

Returns

numpy.ndarray, the final state in numpy array format.

sampling(shots=1, param=None, show=False)[source]

Sampling the bit string based on the final state.

Parameters
  • shots (int) – How many samples you want to get. Default: 1.

  • param (Union[Tensor, numpy.ndarray, ParameterResolver, dict]) – The parameter for the parameterized quantum circuit. If None, the quantum circuit should be a non parameterized quantum circuit. Default: None.

  • show (bool) – Whether to show the sampling result in bar plot. Default: False.

Returns

dict, a dict with key as bit string and value as number of samples.

Examples

>>> from mindquantum.circuit import StateEvolution
>>> from mindquantum.circuit import qft
>>> import numpy as np
>>> np.random.seed(42)
>>> StateEvolution(qft([0, 1])).sampling(100)
{'00': 29, '01': 24, '10': 23, '11': 24}
class mindquantum.circuit.TimeEvolution(ops: mindquantum.ops.qubit_operator.QubitOperator, time=None)[source]

The time evolution operator that can generate a crosponded circuit.

The time evolution operator will do the following evolution:

\[\left|\varphi(t)\right>=e^{-itH}\left|\varphi(0)\right>\]

Note

The hamiltonian should be a parameterized or non parameterized QubitOperator. If the QubitOperator has multiple terms, the first order trotter decomposition will be used.

Parameters
  • ops (QubitOperator) – The qubit operator hamiltonian, could be parameterized or non parameterized.

  • time (Union[numbers.Number, dict, ParameterResolver]) – The evolution time, could be a number or a parameter resolver. If None, the time will be set to 1. Default: None.

Examples

>>> from mindquantum.circuit import TimeEvolution
>>> from mindquantum.ops import QubitOperator
>>> h = QubitOperator('Z0 Z1', 'p')
>>> TimeEvolution(h).circuit
X(1 <-: 0)
RZ(2*p|1)
X(1 <-: 0)
property circuit

Get the first order trotter decomposition circuit of this time evolution operator.

class mindquantum.circuit.U3(a, b, c, obj_qubit=None)[source]

This circuit represent arbitrary single qubit gate.

Parameters

Examples

>>> from mindquantum import U3
>>> U3('a','b','c')
RZ(a|0)
RX(-1.571|0)
RZ(b|0)
RX(1.571|0)
RZ(c|0)
class mindquantum.circuit.UN(gate, maps_obj, coeff=None, maps_ctrl=None)[source]

Map a quantum gate to different objective qubits and control qubits.

Parameters
  • gate (BasicGate) – A quantum gate.

  • maps_obj (Union[int, list[int]]) – Objective qubits. If a int is given and maps_obj is None, then the gate will act on qubit from 0 to this int.

  • coeff (Union[str, List[str], numbers.Number, List[numbers.Number]]) – The parameters for gate (if it is a parameterized gate).

  • maps_ctrl (Union[int, list[int]]) – Control qubits. Default: None.

Returns

Circuit, Return a quantum circuit.

Examples

>>> from mindquantum import UN, RX, H, X, SWAP
>>> circuit1 = UN(X, maps_obj = [0, 1], maps_ctrl = [2, 3])
>>> print(circuit1)
X(0 <-: 2)
X(1 <-: 3)
>>> circuit2 = UN(SWAP, maps_obj =[[0, 1], [2, 3]])
>>> print(circuit2)
SWAP(0 1)
SWAP(2 3)
>>> circuit3 = UN(H, 3)
>>> print(circuit3)
H(0)
H(1)
H(2)
>>> circuit4 = UN(RX, 2, 'a')
>>> print(circuit4)
RX(a_0|0)
RX(a_1|1)
>>> circuit5 = UN(RX, 2, ['a', 'b'])
>>> print(circuit5)
RX(a|0)
RX(b|1)
>>> circuit6 = UN(RX('a'), [0, 3])
>>> print(circuit6)
RX(a|0)
RX(a|3)
class mindquantum.circuit.SwapParts(a: collections.abc.Iterable, b: collections.abc.Iterable, maps_ctrl=None)[source]

Swap two different part of quantum circuit, with or without control qubits.

Parameters
  • a (Iterable) – The first part you need to swap.

  • b (Iterable) – The second part you need to swap.

  • maps_ctrl (int, Iterable) – Control the swap by a single qubit or by different qubits or just no control qubit. Default: None.

Examples

>>> from mindquantum import SwapParts
>>> SwapParts([1, 2], [3, 4], 0)
SWAP(1 3 <-: 0)
SWAP(2 4 <-: 0)
mindquantum.circuit.qft(qubits)[source]

Quantum fourier transform.

Note

Please refer Nielsen, M., & Chuang, I. (2010) for more information.

Parameters

qubits (list[int]) – Qubits you want to apply quantum fourier transform.

Examples

>>> from mindquantum.circuit import qft
>>> from mindquantum.circuit import StateEvolution
>>> print(StateEvolution(qft([0, 1])).final_state(ket=True))
0.5¦00⟩
0.5¦01⟩
0.5¦10⟩
0.5¦11⟩
mindquantum.circuit.pauli_word_to_circuits(qubitops)[source]

Convert a single pauli word qubit operator to a quantum circuit.

Parameters

qubitops (QubitOperator, Hamiltonian) – The single pauli word qubit operator.

Returns

Circuit, a quantum circuit.

Examples

>>> from mindquantum.ops import QubitOperator
>>> qubitops = QubitOperator('X0 Y1')
>>> pauli_word_to_circuits(qubitops)
X(0)
Y(1)
mindquantum.circuit.decompose_single_term_time_evolution(term, para)[source]

Decompose a time evolution gate into basic quantum gates.

This function only work for the hamiltonian with only single pauli word. For example, exp(-i * t * ham), ham can only be a single pauli word, such as ham = X0 x Y1 x Z2, and at this time, term will be ((0, ‘X’), (1, ‘Y’), (2, ‘Z’)). When the evolution time is expressd as t = a*x + b*y, para would be {‘x’:a, ‘y’:b}.

Parameters
Returns

Circuit, a quantum circuit.

Example

>>> from mindquantum.ops import QubitOperator
>>> ham = QubitOperator('X0 Y1')
>>> circuit = decompose_single_term_time_evolution(ham, {'a':1})
>>> print(circuit)
H(0)
RX(1.571,1)
X(1 <-: 0)
RZ(a|1)
X(1 <-: 0)
RX(10.996,1)
H(0)
mindquantum.circuit.generate_uccsd(molecular, th=0)[source]

Generate a uccsd quantum circuit based on a molecular data generated by HiQfermion or openfermion.

Parameters
  • molecular (Union[str, MolecularData]) – the name of the molecular data file, or openfermion MolecularData.

  • th (int) – the threshold to filt the uccsd amplitude. When th < 0, we will keep all amplitudes. When th == 0, we will keep all amplitude that are positive. Default: 0.

Returns

  • uccsd_circuit (Circuit), the ansatz circuit generated by uccsd method.

  • initial_amplitudes (numpy.ndarray), the initial parameter values of uccsd circuit.

  • parameters_name (list[str]), the name of initial parameters.

  • qubit_hamiltonian (QubitOperator), the hamiltonian of the molecule.

  • n_qubits (int), the number of qubits in simulation.

  • n_electrons, the number of electrons of the molecule.

mindquantum.circuit.controlled(circuit_fn)[source]

Add control qubits on a quantum circuit or a quantum operator (a function that can generate a quantum circuit)

Parameters

circuit_fn (Union[Circuit, FunctionType, MethodType]) – A quantum circuit, or a function that can generate a quantum circuit.

Examples

>>> from mindquantum.circuit import qft, controlled
>>> u1 = qft([0, 1])
>>> u2 = controlled(u1)
>>> u3 = controlled(qft)
>>> u3 = u3(2, [0, 1])
>>> u2(2)
H(0 <-: 2)
PS(1.571|0 <-: 1 2)
H(1 <-: 2)
SWAP(0 1 <-: 2)
>>> u3
H(0 <-: 2)
PS(1.571|0 <-: 1 2)
H(1 <-: 2)
SWAP(0 1 <-: 2)
mindquantum.circuit.dagger(circuit_fn)[source]

Get the hermitian dagger of a quantum circuit or a quantum operator (a function that can generate a quantum circuit)

Parameters

circuit_fn (Union[Circuit, FunctionType, MethodType]) – A quantum circuit, or a function that can generate a quantum circuit.

Examples

>>> from mindquantum.circuit import qft, dagger
>>> u1 = qft([0, 1])
>>> u2 = dagger(u1)
>>> u3 = dagger(qft)
>>> u3 = u3([0, 1])
>>> u2
SWAP(0 1)
H(1)
PS(-1.571|0 <-: 1)
H(0)
>>> u3
SWAP(0 1)
H(1)
PS(-1.571|0 <-: 1)
H(0)
mindquantum.circuit.apply(circuit_fn, qubits)[source]

Apply a quantum circuit or a quantum operator (a function that can generate a quantum circuit) to different qubits.

Parameters
  • circuit_fn (Union[Circuit, FunctionType, MethodType]) – A quantum circuit, or a function that can generate a quantum circuit.

  • qubits (list[int]) – The new qubits that you want to apply.

Examples

>>> from mindquantum.circuit import qft, apply
>>> u1 = qft([0, 1])
>>> u2 = apply(u1, [1, 2])
>>> u3 = apply(qft, [1, 2])
>>> u3 = u3([0, 1])
>>> u2
H(1)
PS(1.571|1 <-: 2)
H(2)
SWAP(1 2)
>>> u3
H(1)
PS(1.571|1 <-: 2)
H(2)
SWAP(1 2)
mindquantum.circuit.add_prefix(circuit_fn, prefix)[source]

Add a prefix on the parameter of a parameterized quantum circuit or a parameterized quantum operator (a function that can generate a parameterized quantum circuit).

Parameters
  • circuit_fn (Union[Circuit, FunctionType, MethodType]) – A quantum circuit, or a function that can generate a quantum circuit.

  • prefix (str) – The prefix you want to add to every parameters.

Examples

>>> from mindquantum.circuit import qft, add_prefix
>>> from mindquantum import RX, H, Circuit
>>> u = lambda qubit: Circuit([H.on(0), RX('a').on(qubit)])
>>> u1 = u(0)
>>> u2 = add_prefix(u1, 'ansatz')
>>> u3 = add_prefix(u, 'ansatz')
>>> u3 = u3(0)
>>> u2
H(0)
RX(ansatz_a|0)
>>> u3
H(0)
RX(ansatz_a|0)
mindquantum.circuit.change_param_name(circuit_fn, name_map)[source]

Change the parameter name of a parameterized quantum circuit or a parameterized quantum operator (a function that can generate a parameterized quantum circuit).

Parameters
  • circuit_fn (Union[Circuit, FunctionType, MethodType]) – A quantum circuit, or a function that can generate a quantum circuit.

  • name_map (dict) – The parameter name mapping dict.

Examples

>>> from mindquantum.circuit import qft, change_param_name
>>> from mindquantum import RX, H, Circuit
>>> u = lambda qubit: Circuit([H.on(0), RX('a').on(qubit)])
>>> u1 = u(0)
>>> u2 = change_param_name(u1, {'a': 'b'})
>>> u3 = change_param_name(u, {'a': 'b'})
>>> u3 = u3(0)
>>> u2
H(0)
RX(b|0)
>>> u3
H(0)
RX(b|0)

functional

The functional operators are shortcut of some pre-instantiated quantum circuit operators.

functional

high level operators

mindquantum.circuit.C

mindquantum.circuit.controlled

mindquantum.circuit.D

mindquantum.circuit.dagger

mindquantum.circuit.A

mindquantum.circuit.apply

mindquantum.circuit.AP

mindquantum.circuit.add_prefix

mindquantum.circuit.CPN

mindquantum.circuit.change_param_name

mindquantum.engine

Circuit engine module.

class mindquantum.engine.BasicQubit(qubit_id, circuit=None)[source]

A quantum qubit.

Parameters
  • qubit_id (int) – The id of this quantum qubit.

  • circuit (Circuit) – The quantum circuit that this qubit belongs to. Default: None.

property circuit

Get the quantum circuit that this qubit belongs to.

class mindquantum.engine.CircuitEngine[source]

A simple circuit engine that allows you to generate quantum circuit as projectq style.

Note

For more usage, please refers to CircuitEngine.generator

allocate_qubit()[source]

Allocate a quantum qubit.

allocate_qureg(n)[source]

Allocate a quantum register.

Parameters

n (int) – Number of quantum qubits.

property circuit

Get the quantum circuit that construct by this engin.

static generator(n_qubits, *args, **kwds)[source]

Quantum circuit register.

Parameters

n_qubits (int) – qubit number of quantum circuit.

Examples

>>> import mindquantum.gate as G
>>> from mindquantum.engine import circuit_generator
>>> @circuit_generator(2,prefix='p')
>>> def ansatz(qubits, prefix):
>>>     G.X | (qubits[0], qubits[1])
>>>     G.RX(prefix+'_0') | qubits[1]
>>> print(ansatz)
X(1 <-: 0)
RX(p_0|1)
>>> print(type(ansatz))
<class 'mindquantum.circuit.circuit.Circuit'>
mindquantum.engine.circuit_generator(n_qubits, *args, **kwds)[source]

Generate quantum circuit as projectq style.

Parameters

n_qubits (int) – qubit number of quantum circuit.

Note

More information please refers to CircuitEngine.generator.