mindquantum.utils.random_insert_gates
- mindquantum.utils.random_insert_gates(circuit: Circuit, gates: BasicGate | list[BasicGate], nums: int | list[int], focus_on: int | list[int] | None = None, with_ctrl: bool = True, after_measure: bool = False, shots: int = 1, seed: int | None = None)[source]
Randomly insert given numbers of single-qubit gates into a quantum circuit.
- Parameters
circuit (Circuit) – The circuit with gates to be inserted into.
gates (Union[BasicGate, List[BasicGate]]) – The selected single-qubit gates to be inserted.
nums (Union[int, List[int]]) – The number of each gate to be inserted. Note that the length of the nums should be equal to that of the gates.
with_ctrl (bool, optional) – Whether insert gates for control qubits. Default:
True
.focus_on (Union[int, List[int]], optional) – only insert gates on
focus_on
qubits. IfNone
, insert to all qubits of selected gates. Default:None
.after_measure (bool, optional) – Whether insert gates after measure gates. Default:
False
.shots (int, optional) – How many shots you want to sampling this circuit. Default:
1
.seed (int, optional) – Random seed for random sampling. If
None
, seed will be a random int number. Default:None
.
- Returns
A generator that can generate quantum circuits.
Examples
>>> from mindquantum.core.gates import X, Z, BitFlipChannel, PhaseFlipChannel >>> from mindquantum.core.circuit import Circuit >>> from mindquantum.utils import random_insert_gates >>> origin = Circuit().rx('theta', 0).rz('beta', 1, 0).barrier().measure(0) >>> print(origin) ┏━━━━━━━━━━━┓ ┍━━━━━━┑ q0: ──┨ RX(theta) ┠───────■──────▓─┤ M q0 ├─── ┗━━━━━━━━━━━┛ ┃ ▓ ┕━━━━━━┙ ┏━━━━━┻━━━━┓ ▓ q1: ────────────────┨ RZ(beta) ┠─▓──────────── ┗━━━━━━━━━━┛ >>> circs = list(random_insert_gates(origin, [BitFlipChannel(p=1), PhaseFlipChannel(p=1)], [2, 1])) >>> print(circs[0]) ┏━━━━━━━━━━━┓ ╔══════════╗ ╔══════════╗ ┍━━━━━━┑ q0: ──┨ RX(theta) ┠─╢ BFC(p=1) ╟─╢ PFC(p=1) ╟───────■───────────────────▓─┤ M q0 ├─── ┗━━━━━━━━━━━┛ ╚══════════╝ ╚══════════╝ ┃ ▓ ┕━━━━━━┙ ┏━━━━━┻━━━━┓ ╔══════════╗ ▓ q1: ──────────────────────────────────────────┨ RZ(beta) ┠─╢ BFC(p=1) ╟─▓──────────── ┗━━━━━━━━━━┛ ╚══════════╝