mindquantum.core.circuit.Circuit
- class mindquantum.core.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. Default:
None
.
Examples
>>> from mindquantum.core.circuit import Circuit >>> from mindquantum.core.gates import RX, X >>> circuit1 = Circuit() >>> circuit1 += RX('a').on(0) >>> circuit1 *= 2 >>> circuit1 ┏━━━━━━━┓ ┏━━━━━━━┓ q0: ──┨ RX(a) ┠─┨ RX(a) ┠─── ┗━━━━━━━┛ ┗━━━━━━━┛ >>> circuit2 = Circuit([X.on(0,1)]) >>> circuit3= circuit1 + circuit2 >>> assert len(circuit3) == 3 >>> circuit3.summary() Circuit Summary ╭──────────────────────┬───────╮ │ Info │ value │ ├──────────────────────┼───────┤ │ Number of qubit │ 2 │ ├──────────────────────┼───────┤ │ Total number of gate │ 3 │ │ Barrier │ 0 │ │ Noise Channel │ 0 │ │ Measurement │ 0 │ ├──────────────────────┼───────┤ │ Parameter gate │ 2 │ │ 1 ansatz parameter │ a │ ╰──────────────────────┴───────╯ >>> circuit3 ┏━━━━━━━┓ ┏━━━━━━━┓ ┏━━━┓ q0: ──┨ RX(a) ┠─┨ RX(a) ┠─┨╺╋╸┠─── ┗━━━━━━━┛ ┗━━━━━━━┛ ┗━┳━┛ ┃ q1: ────────────────────────■───── >>> Circuit.display_detail(False) >>> circuit3 ┏━━━━┓┏━━━━┓┏━━━┓ q0: ─┨ RX ┠┨ RX ┠┨╺╋╸┠─── ┗━━━━┛┗━━━━┛┗━┳━┛ ┃ q1: ───────────────■─────
- property ansatz_params_name
Get the ansatz parameter name of this circuit.
- Returns
list, a list that contains the parameter name that works as ansatz.
Examples
>>> from mindquantum.core.gates import RX, RY >>> from mindquantum.core.circuit import Circuit >>> circuit = Circuit(RX({'a': 1, 'b': 2}).on(0)).as_encoder() >>> circuit += Circuit(RY('c').on(0)).as_ansatz() >>> circuit.ansatz_params_name ['c']
- 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.core.gates import X, RX >>> from mindquantum.core.circuit import Circuit >>> circuit = Circuit() >>> circuit += X.on(0) >>> circuit += RX({'a': 2}).on(0) >>> circuit = circuit.apply_value({'a': 1.5}) >>> circuit ┏━━━┓ ┏━━━━━━━┓ q0: ──┨╺╋╸┠─┨ RX(3) ┠─── ┗━━━┛ ┗━━━━━━━┛
- as_ansatz(inplace=True)[source]
To set this circuit to ansatz or not.
- Parameters
inplace (bool) – Whether to set inplace. Defaults:
True
.
- as_encoder(inplace=True)[source]
To set this circuit to encoder.
- Parameters
inplace (bool) – Whether to set inplace. Defaults:
True
.
- barrier(show=True)[source]
Add a barrier.
- Parameters
show (bool) – Whether show barrier or not. Default: True.
- compress()[source]
Remove all unused qubits, and map qubits to range(n_qubits).
Examples
>>> from mindquantum.algorithm.library import qft >>> qft([0, 2, 4]) ┏━━━┓ ┏━━━━━━━━━┓ ┏━━━━━━━━━┓ q0: ──┨ H ┠─┨ PS(π/2) ┠─┨ PS(π/4) ┠─────────────────────────╳─── ┗━━━┛ ┗━━━━┳━━━━┛ ┗━━━━┳━━━━┛ ┃ ┃ ┃ ┏━━━┓ ┏━━━━━━━━━┓ ┃ q2: ─────────────■───────────╂──────┨ H ┠─┨ PS(π/2) ┠───────┃─── ┃ ┗━━━┛ ┗━━━━┳━━━━┛ ┃ ┃ ┃ ┏━━━┓ ┃ q4: ─────────────────────────■─────────────────■──────┨ H ┠─╳─── ┗━━━┛ >>> qft([0, 2, 4]).compress() ┏━━━┓ ┏━━━━━━━━━┓ ┏━━━━━━━━━┓ q0: ──┨ H ┠─┨ PS(π/2) ┠─┨ PS(π/4) ┠─────────────────────────╳─── ┗━━━┛ ┗━━━━┳━━━━┛ ┗━━━━┳━━━━┛ ┃ ┃ ┃ ┏━━━┓ ┏━━━━━━━━━┓ ┃ q1: ─────────────■───────────╂──────┨ H ┠─┨ PS(π/2) ┠───────┃─── ┃ ┗━━━┛ ┗━━━━┳━━━━┛ ┃ ┃ ┃ ┏━━━┓ ┃ q2: ─────────────────────────■─────────────────■──────┨ H ┠─╳─── ┗━━━┛
- static display_detail(state: bool)[source]
Whether to display the detail of circuit.
- Parameters
state (bool) – The state of whether to display the detail of circuit.
Examples
>>> from mindquantum import Circuit >>> circ = Circuit().rx('a', 0).ry(1.2, 0) >>> circ ┏━━━━━━━┓ ┏━━━━━━━━━┓ q0: ──┨ RX(a) ┠─┨ RY(6/5) ┠─── ┗━━━━━━━┛ ┗━━━━━━━━━┛ >>> Circuit.display_detail(False) >>> circ ┏━━━━┓┏━━━━┓ q0: ─┨ RX ┠┨ RY ┠─── ┗━━━━┛┗━━━━┛
- property encoder_params_name
Get the encoder parameter name of this circuit.
- Returns
list, a list that contains the parameter name that works as encoder.
Examples
>>> from mindquantum.core.gates import RX, RY >>> from mindquantum.core.circuit import Circuit >>> circuit = Circuit(RX({'a': 1, 'b': 2}).on(0)).as_encoder() >>> circuit += Circuit(RY('c').on(0)).as_ansatz() >>> circuit.encoder_params_name ['a', 'b']
- static from_hiqasm(hiqasm_str: str)[source]
Convert a HiQASM string or a HiQASM file to MindQuantum circuit.
- static from_openqasm(openqasm_str: str)[source]
Convert an OpenQASM string or an OpenQASM file to MindQuantum circuit.
- fsim(theta, phi, obj_qubits, ctrl_qubits=None)[source]
Add a FSim gate.
- Parameters
theta (Union[dict, ParameterResolver]) – First parameter for FSim gate.
phi (Union[dict, ParameterResolver]) – Second parameter for FSim gate.
obj_qubits (Union[int, list[int]]) – The object qubits of FSim gate.
ctrl_qubits (Union[int, list[int]]) – the control qubits of FSim gate. Default:
None
.
- get_cpp_obj(hermitian=False)[source]
Get cpp obj of circuit.
- Parameters
hermitian (bool) – Whether to get cpp object of this circuit in hermitian version. Default:
False
.
- get_qs(backend='mqvector', pr=None, ket=False, seed=None, dtype=None)[source]
Get the final quantum state of this circuit.
- Parameters
backend (str) – Which backend you want to use. Default:
'mqvector'
.pr (Union[numbers.Number, ParameterResolver, dict, numpy.ndarray]) – The parameter of this circuit, if this circuit is parameterized. Default:
None
.ket (str) – Whether to return the quantum state in ket format. Default:
False
.seed (int) – The random seed of simulator. Default:
None
dtype (mindquantum.dtype) – The data type of simulator.
- property has_measure_gate
To check whether this circuit has measure gate.
- Returns
bool, whether this circuit has measure gate.
- hermitian()[source]
Get the hermitian of this quantum circuit.
Examples
>>> from mindquantum.core.circuit import Circuit >>> from mindquantum.core.gates import RX >>> circ = Circuit(RX({'a': 0.2}).on(0)) >>> herm_circ = circ.hermitian() >>> print(herm_circ) ┏━━━━━━━━━━━━┓ q0: ──┨ RX(-1/5*a) ┠─── ┗━━━━━━━━━━━━┛
- property is_measure_end
Check whether each qubit has a measurement as its last operation.
Check whether the circuit is end with measurement gate that there is at most one measurement gate that act on each qubit, and this measurement gate should be at end of gate serial of this qubit.
- Returns
bool, whether the circuit is end with measurement.
- property is_noise_circuit
To check whether this circuit has noise channel.
- Returns
bool, whether this circuit has noise channel.
- matrix(pr=None, big_end=False, backend='mqvector', seed=None, dtype=None)[source]
Get the matrix of this circuit.
- Parameters
pr (ParameterResolver, dict, numpy.ndarray, list, numbers.Number) – The parameter resolver for parameterized quantum circuit. Default: None.
big_end (bool) – The low index qubit is place in the end or not. Default: False.
backend (str) – The backend to do simulation. Default: ‘mqvector’.
seed (int) – The random to generate circuit matrix, if the circuit has noise channel.
dtype (mindquantum.dtype) – data type of simulator. Default: ‘None’.
- Returns
numpy.ndarray, two dimensional complex matrix of this circuit.
Examples
>>> from mindquantum.core.circuit import Circuit >>> circuit = Circuit().rx('a',0).h(0) >>> circuit.matrix({'a': 1.0}) array([[ 0.62054458-0.33900505j, 0.62054458-0.33900505j], [ 0.62054458+0.33900505j, -0.62054458-0.33900505j]])
- measure(key, obj_qubit=None, reset_to=None)[source]
Add a measure gate.
- Parameters
key (Union[int, str]) – If obj_qubit is
None
, then key should be a int and means which qubit to measure, otherwise, key should be a str and means the name of this measure gate.obj_qubit (int) – Which qubit to measure. Default:
None
.reset_to (Union[int, None]) – Reset the qubit to 0 state or 1 state. If
None
, do not reset. Default:None
.
- property n_qubits
Get the total number of qubits used.
- 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.
- property parameterized
To check whether this circuit is a parameterized quantum circuit.
- Returns
bool, whether this circuit is a parameterized quantum circuit.
- property params_name
Get the parameter name of this circuit.
- Returns
list, a list that contains the parameter name.
Examples
>>> from mindquantum.core.gates import RX >>> from mindquantum.core.circuit import Circuit >>> circuit = Circuit(RX({'a': 1, 'b': 2}).on(0)) >>> circuit.params_name ['a', 'b']
- remove_measure_on_qubits(qubits)[source]
Remove all measure gate on some certain qubits.
Examples
>>> from mindquantum.core.circuit import UN >>> from mindquantum.core.gates import H, Measure >>> circ = UN(H, 3).x(0, 1).x(1, 2).measure_all() >>> circ += H.on(0) >>> circ += Measure('q0_1').on(0) >>> circ.remove_measure_on_qubits(0) ┏━━━┓ ┏━━━┓ ┏━━━┓ q0: ──┨ H ┠─┨╺╋╸┠─┨ H ┠──────────── ┗━━━┛ ┗━┳━┛ ┗━━━┛ ┏━━━┓ ┃ ┏━━━┓ ┍━━━━━━┑ q1: ──┨ H ┠───■───┨╺╋╸┠─┤ M q1 ├─── ┗━━━┛ ┗━┳━┛ ┕━━━━━━┙ ┏━━━┓ ┃ ┍━━━━━━┑ q2: ──┨ H ┠─────────■───┤ M q2 ├─── ┗━━━┛ ┕━━━━━━┙
- reverse_qubits()[source]
Flip the circuit to big endian.
Examples
>>> from mindquantum.core.circuit import Circuit >>> circ = Circuit().h(0).x(2, 0).y(3).x(3, 2) >>> circ ┏━━━┓ q0: ──┨ H ┠───■─────────── ┗━━━┛ ┃ ┏━┻━┓ q2: ────────┨╺╋╸┠───■───── ┗━━━┛ ┃ ┏━━━┓ ┏━┻━┓ q3: ──┨ Y ┠───────┨╺╋╸┠─── ┗━━━┛ ┗━━━┛ >>> circ.reverse_qubits() ┏━━━┓ ┏━━━┓ q0: ──┨ Y ┠───────┨╺╋╸┠─── ┗━━━┛ ┗━┳━┛ ┏━━━┓ ┃ q1: ────────┨╺╋╸┠───■───── ┗━┳━┛ ┏━━━┓ ┃ q3: ──┨ H ┠───■─────────── ┗━━━┛
- summary(show=True)[source]
Print a summary of the current circuit.
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.core.circuit import Circuit >>> from mindquantum.core.gates import RX, H >>> circuit = Circuit([RX('a').on(1), H.on(1), RX('b').on(0)]) >>> circuit.summary() Circuit Summary ╭──────────────────────┬───────╮ │ Info │ value │ ├──────────────────────┼───────┤ │ Number of qubit │ 2 │ ├──────────────────────┼───────┤ │ Total number of gate │ 3 │ │ Barrier │ 0 │ │ Noise Channel │ 0 │ │ Measurement │ 0 │ ├──────────────────────┼───────┤ │ Parameter gate │ 2 │ │ 2 ansatz parameters │ a, b │ ╰──────────────────────┴───────╯
- svg(style=None, width=None, scale=None)[source]
Display current quantum circuit into SVG picture in jupyter notebook.
- to_hiqasm(file_name: Optional[str] = None, version: str = '0.1')[source]
Convert a MindQuantum circuit to HiQASM format string or file.
- to_openqasm(file_name: Optional[str] = None, version: str = '2.0')[source]
Convert a MindQuantum circuit to OpenQASM format string or file.
- u3(theta, phi, lamda, obj_qubits, ctrl_qubits=None)[source]
Add a U3 gate.
- Parameters
theta (Union[dict, ParameterResolver]) – First parameter for U3 gate.
phi (Union[dict, ParameterResolver]) – Second parameter for U3 gate.
lamda (Union[dict, ParameterResolver]) – Third parameter for U3 gate.
obj_qubits (Union[int, list[int]]) – The object qubits of U3 gate.
ctrl_qubits (Union[int, list[int]]) – the control qubits of U3 gate. Default:
None
.
- un(gate, maps_obj, maps_ctrl=None)[source]
Map a quantum gate to different objective qubits and control qubits.
Please refer to UN.
- with_noise(noise_gate=mq_gates.AmplitudeDampingChannel(0.001), also_ctrl=False)[source]
Apply noises on each gate.