mindquantum.core.gates

Quantum Gates

Gate.

Gate provides different quantum gate.

class mindquantum.core.gates.BarrierGate(show=True)[source]

BARRIER gate do nothing but set a barrier for drawing circuit.

Parameters

show (bool) – whether show this barrier gate. Default: True.

Raises

TypeError – if show is not bool.

class mindquantum.core.gates.BasicGate(name, parameterized=False)[source]

BasicGate is the base class of all gates.

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

  • parameterized (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.core.gates 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.core.gates.BitFlipChannel(p: float)[source]

Quantum channel that express the incoherent noise in quantum computing.

Bit flip channel express error that randomly flip the qubit (applies X gate) with probability P, or do noting (applies I gate) with probability 1 - P.

Bit flip channel applies noise as:

\[\epsilon(\rho) = (1 - P)\rho + P X \rho X\]

where ρ is quantum state as density matrix type; P is the probability of applying an additional X gate.

Parameters

p (int, float) – probability of occurred error.

Examples

>>> from mindquantum.core.gates import BitFlipChannel
>>> from mindquantum.core.circuit import Circuit
>>> circ = Circuit()
>>> circ += BitFlipChannel(0.02).on(0)
>>> circ += BitFlipChannel(0.01).on(1, 0)
>>> print(circ)
q0: ──BFC─────●───

q1: ─────────BFC──
class mindquantum.core.gates.BitPhaseFlipChannel(p: float)[source]

Quantum channel that express the incoherent noise in quantum computing.

Bit phase flip channel express error that randomly flip both the state and phase of qubit (applies Y gate) with probability P, or do noting (applies I gate) with probability 1 - P.

Bit phase flip channel applies noise as:

\[\epsilon(\rho) = (1 - P)\rho + P Y \rho Y\]

where ρ is quantum state as density matrix type; P is the probability of applying an additional Y gate.

Parameters

p (int, float) – probability of occurred error.

Examples

>>> from mindquantum.core.gates import BitPhaseFlipChannel
>>> from mindquantum.core.circuit import Circuit
>>> circ = Circuit()
>>> circ += BitPhaseFlipChannel(0.02).on(0)
>>> circ += BitPhaseFlipChannel(0.01).on(1, 0)
>>> print(circ)
q0: ──BPFC─────●────

q1: ──────────BPFC──
class mindquantum.core.gates.CNOTGate[source]

Control-X gate.

More usage, please see mindquantum.core.gates.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.core.gates.DepolarizingChannel(p: float)[source]

Quantum channel that express the incoherent noise in quantum computing.

Depolarizing channel express errors that have probability P to turn qubit’s quantum state into maximally mixed state, by randomly applying one of the pauli gate(X,Y,Z) with same probability P/3. And it has probability 1 - P to change nothing (applies I gate).

Depolarizing channel applies noise as:

\[\epsilon(\rho) = (1 - P)\rho + P/3( X \rho X + Y \rho Y + Z \rho Z)\]

where ρ is quantum state as density matrix type; P is the probability of occurred the depolarizing error.

Parameters

p (int, float) – probability of occurred error.

Examples

>>> from mindquantum.core.gates import DepolarizingChannel
>>> from mindquantum.core.circuit import Circuit
>>> circ = Circuit()
>>> circ += DepolarizingChannel(0.02).on(0)
>>> circ += DepolarizingChannel(0.01).on(1, 0)
>>> print(circ)
q0: ──DC────●───

q1: ────────DC──
class mindquantum.core.gates.GlobalPhase(coeff=None)[source]

Global phase gate. More usage, please see mindquantum.core.gates.RX.

\[\begin{split}{\rm GlobalPhase}=\begin{pmatrix}\exp(-i\theta)&0\\ 0&\exp(-i\theta)\end{pmatrix}\end{split}\]
Parameters

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

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.core.gates.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.core.gates.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.core.gates.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.core.gates.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.core.gates.ISWAPGate[source]

ISWAP gate that swap two different qubits and phase the \(\left|01\right>\) and \(\left|10\right>\) amplitudes by \(i\).

More usage, please see mindquantum.core.gates.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.core.gates.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.core.gates import RX
>>> rx1 = RX(1.2)
>>> rx1
RX(6/5)
>>> 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  ]])
get_cpp_obj()[source]

Get cpp obj of this gate.

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.core.gates 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.core.gates.Measure(name='')[source]

Measurement gate that measure quantum qubits.

Parameters

name (str) – The key of this measurement gate. In a quantum circuit, the key of different measurement gate should be unique. Default: “”

Examples

>>> import numpy as np
>>> from mindquantum import qft, Circuit
>>> from mindquantum import Measure
>>> from mindquantum import Simulator
>>> circ = qft(range(2))
>>> circ += Measure('q0').on(0)
>>> circ += Measure().on(1)
>>> circ
q0: ──H────PS(π/2)─────────@────M(q0)──
              │            │
q1: ──────────●───────H────@────M(q1)──
>>> sim = Simulator('projectq', circ.n_qubits)
>>> sim.apply_circuit(Circuit().h(0).x(1, 0))
>>> sim
projectq simulator with 2 qubits (little endian).
Current quantum state:
√2/2¦00⟩
√2/2¦11⟩
>>> res = sim.sampling(circ, shots=2000, seed=42)
>>> res
shots: 2000
Keys: q1 q0│0.00   0.124       0.248       0.372       0.496       0.621
───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴
         00│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

         10│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

         11│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

{'00': 993, '10': 506, '11': 501}
>>> sim
projectq simulator with 2 qubits (little endian).
Current quantum state:
√2/2¦00⟩
√2/2¦11⟩
>>> sim.apply_circuit(circ[:-2])
>>> sim
projectq simulator with 2 qubits (little endian).
Current quantum state:
√2/2¦00⟩
(√2/4-√2/4j)¦10⟩
(√2/4+√2/4j)¦11⟩
>>> np.abs(sim.get_qs())**2
array([0.5 , 0.  , 0.25, 0.25])
hermitian()[source]

Hermitian gate of measure return its self

on(obj_qubits, ctrl_qubits=None)[source]

Apply this measurement gate on which qubit.

Parameters
  • obj_qubits (int) – A non negative int that referring to its index number.

  • ctrl_qubits (int) – Should be None for measure gate. Default: None.

Examples

>>> from mindquantum import Circuit, Measure
>>> from mindquantum import Simulator
>>> sim = Simulator('projectq', 2)
>>> circ = Circuit().h(0).x(1, 0)
>>> circ += Measure('q0').on(0)
>>> circ += Measure('q1').on(1)
>>> circ
q0: ──H────●────M(q0)──

q1: ───────X────M(q1)──
>>> res = sim.apply_circuit(circ)
>>> res
shots: 1
Keys: q1 q0│0.00     0.2         0.4         0.6         0.8         1.0
───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴
         11│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

{'11': 1}
>>> sim
projectq simulator with 2 qubits  (little endian).
Current quantum state:
1¦11⟩
class mindquantum.core.gates.MeasureResult[source]

Measurement result container

Examples

>>> from mindquantum import qft
>>> from mindquantum import Simulator
>>> sim = Simulator('projectq', 2)
>>> res = sim.sampling(qft(range(2)).measure_all(), shots=1000, seed=42)
>>> res
shots: 1000
Keys: q1 q0│0.00   0.065        0.13       0.194       0.259       0.324
───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴
         00│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

         01│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

         10│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

         11│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

{'00': 230, '01': 254, '10': 257, '11': 259}
>>> res.data
{'00': 230, '01': 254, '10': 257, '11': 259}
add_measure(measure)[source]

Add a measurement gate into this measurement result container. Measure key should be unique in this measurement result container.

Parameters

measure (Union[Iterable, Measure]) – One or more measure gates.

collect_data(samples)[source]

collect the measured bit string

Parameters

samples (numpy.ndarray) – A two dimensional (N x M) numpy array that stores the sampling bit string in 0 or 1, where N represents the number of shot times, and M represents the number of keys in this measurement container

property data

Get the sampling data.

Returns

The samping data.

Return type

dict

select_keys(*keys)[source]

Select certain measurement keys from this measurement container

Parameters

keys (tuple[str]) – The key you want to select.

Examples

>>> from mindquantum import Simulator
>>> from mindquantum import qft, H
>>> circ = qft(range(2)).measure('q0_0', 0).measure('q1_0', 1)
>>> circ.h(0).measure('q0_1', 0)
>>> circ
q0: ──H────PS(π/2)─────────@────M(q0_0)────H────M(q0_1)──
              │            │
q1: ──────────●───────H────@────M(q1_0)──────────────────
>>> sim = Simulator('projectq', circ.n_qubits)
>>> res = sim.sampling(circ, shots=500, seed=42)
>>> new_res = res.select_keys('q0_1', 'q1_0')
>>> new_res
shots: 500
Keys: q1_0 q0_1│0.00   0.068       0.136       0.204       0.272        0.34
───────────────┼───────────┴───────────┴───────────┴───────────┴───────────┴
             00│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

             01│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

             10│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

             11│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

{'00': 127, '01': 107, '10': 136, '11': 130}
svg(style=None)[source]

Display current measurement result into SVG picture in jupyter notebook.

Parameters

style (dict, str) – the style to set svg style. Currently, we support ‘official’. Default: None.

class mindquantum.core.gates.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

get_cpp_obj()[source]

Get the cpp obj of this gate.

hermitian()[source]

Get hermitian gate of this none parameterized gate.

matrix(*args)[source]

Get the matrix of this none parameterized gate.

class mindquantum.core.gates.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

BasicGate, 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

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

requires_grad()[source]

All parameters requires grad. Inplace operation.

Returns

BasicGate, 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

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

class mindquantum.core.gates.PauliChannel(px: float, py: float, pz: float)[source]

Quantum channel that express the incoherent noise in quantum computing.

Pauli channel express error that randomly applies an additional X, Y or Z gate on qubits with different probabilities Px, Py and Pz, or do noting (applies I gate) with probability P = (1 - Px - Py - Pz).

Pauli channel applies noise as:

\[\epsilon(\rho) = (1 - P_x - P_y - P_z)\rho + P_x X \rho X + P_y Y \rho Y + P_z Z \rho Z\]

where ρ is quantum state as density matrix type; Px, Py and Pz is the probability of applying an additional X, Y and Z gate.

Parameters
  • px (int, float) – probability of applying X gate.

  • py (int, float) – probability of applying Y gate.

  • pz (int, float) – probability of applying Z gate.

Examples

>>> from mindquantum.core.gates import PauliChannel
>>> from mindquantum.core.circuit import Circuit
>>> circ = Circuit()
>>> circ += PauliChannel(0.8, 0.1, 0.1).on(0)
>>> circ += PauliChannel(0, 0.05, 0.9).on(1, 0)
>>> circ.measure_all()
>>> print(circ)
q0: ──PC────●─────M(q0)──

q1: ────────PC────M(q1)──
>>> from mindquantum.simulator import Simulator
>>> sim = Simulator('projectq', 2)
>>> sim.sampling(circ, shots=1000, seed=42)
shots: 1000
Keys: q1 q0│0.00     0.2         0.4         0.6         0.8         1.0
───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴
         00│▒▒▒▒▒▒▒

         01│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

         11│▒▒▒

{'00': 101, '01': 862, '11': 37}
define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.core.gates.PhaseFlipChannel(p: float)[source]

Quantum channel that express the incoherent noise in quantum computing.

Phase flip channel express error that randomly flip the phase of qubit (applies Z gate) with probability P, or do noting (applies I gate) with probability 1 - P.

Phase flip channel applies noise as:

\[\epsilon(\rho) = (1 - P)\rho + P Z \rho Z\]

where ρ is quantum state as density matrix type; P is the probability of applying an additional Z gate.

Parameters

p (int, float) – probability of occurred error.

Examples

>>> from mindquantum.core.gates import PhaseFlipChannel
>>> from mindquantum.core.circuit import Circuit
>>> circ = Circuit()
>>> circ += PhaseFlipChannel(0.02).on(0)
>>> circ += PhaseFlipChannel(0.01).on(1, 0)
>>> print(circ)
q0: ──PFC─────●───

q1: ─────────PFC──
class mindquantum.core.gates.PhaseShift(coeff=None)[source]

Phase shift gate. More usage, please see mindquantum.core.gates.RX.

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

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

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.core.gates.Power(gate: 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.core.gates.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, ParameterResolver]) – the parameters of parameterized gate, see above for detail explanation. Default: None.

Examples

>>> from mindquantum.core.gates 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(0.2*a + 0.5*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.core.gates.RY(coeff=None)[source]

Rotation gate around y-axis. More usage, please see mindquantum.core.gates.RX.

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

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

define_projectq_gate()[source]

Define the corresponded projectq gate.

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

Rotation gate around z-axis. More usage, please see mindquantum.core.gates.RX.

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

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

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.core.gates.SGate[source]

S gate with matrix as :

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

More usage, please see mindquantum.core.gates.XGate.

class mindquantum.core.gates.SWAPGate[source]

SWAP gate that swap two different qubits.

More usage, please see mindquantum.core.gates.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.core.gates.TGate[source]

T gate with matrix as :

\[\begin{split}{\rm T}=\begin{pmatrix}1&0\\0&(1+i)/\sqrt(2)\end{pmatrix}\end{split}\]

More usage, please see mindquantum.core.gates.XGate.

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

Universal math gate.

More usage, please see mindquantum.core.gates.XGate.

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

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

Examples

>>> from mindquantum.core.gates 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.core.gates.XGate[source]

Pauli X gate with matrix as:

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

For simplicity, we define `X` as a instance of `XGate()`. For more redefine, please refer the functional table below.

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.core.gates 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(2π)
>>> (x1**'a').coeff
{'a': 3.141592653589793}
>>> (x1**{'a' : 2}).coeff
{'a': 6.283185307179586}
define_projectq_gate()[source]

Define the corresponded projectq gate.

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

Ising XX gate. More usage, please see mindquantum.core.gates.RX.

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

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

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.core.gates.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.core.gates.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

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

Ising YY gate. More usage, please see mindquantum.core.gates.RX.

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

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

define_projectq_gate()[source]

Define the corresponded projectq gate.

class mindquantum.core.gates.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.core.gates.XGate.

define_projectq_gate()[source]

Define the corresponded projectq gate.

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

Ising ZZ gate. More usage, please see mindquantum.core.gates.RX.

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

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

define_projectq_gate()[source]

Define the corresponded projectq gate.

mindquantum.core.gates.gene_univ_parameterized_gate(name, matrix_generator, diff_matrix_generator)[source]

Generate a customer parameterized gate based on the single parameter defined unitary matrix.

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

  • matrix_generator (Union[FunctionType, MethodType]) – A function or a method that take exactly one argument to generate a unitary matrix.

  • diff_matrix_generator (Union[FunctionType, MethodType]) – A function or a method that take exactly one argument to generate the derivative of this unitary matrix.

Returns

_UnivParameterizedGate, a customer parameterized gate.

Examples

>>> import numpy as np
>>> from mindquantum import gene_univ_parameterized_gate
>>> from mindquantum import Simulator, Circuit
>>> def matrix(theta):
...     return np.array([[np.exp(1j * theta), 0],
...                      [0, np.exp(-1j * theta)]])
>>> def diff_matrix(theta):
...     return 1j*np.array([[np.exp(1j * theta), 0],
...                         [0, -np.exp(-1j * theta)]])
>>> TestGate = gene_univ_parameterized_gate('Test', matrix, diff_matrix)
>>> circ = Circuit().h(0)
>>> circ += TestGate('a').on(0)
>>> circ
q0: ──H────Test(a)──
>>> circ.get_qs(pr={'a': 1.2})
array([0.25622563+0.65905116j, 0.25622563-0.65905116j])

functional

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

functional

gates

mindquantum.core.gates.CNOT

mindquantum.core.gates.CNOTGate

mindquantum.core.gates.I

mindquantum.core.gates.IGate

mindquantum.core.gates.H

mindquantum.core.gates.HGate

mindquantum.core.gates.S

mindquantum.core.gates.PhaseShift (numpy.pi/2)

mindquantum.core.gates.SWAP

mindquantum.core.gates.SWAPGate

mindquantum.core.gates.X

mindquantum.core.gates.XGate

mindquantum.core.gates.Y

mindquantum.core.gates.YGate

mindquantum.core.gates.Z

mindquantum.core.gates.ZGate