deepquantum package#
Subpackages#
- deepquantum.mbqc package- Submodules
- deepquantum.mbqc.command module
- deepquantum.mbqc.operation module
- deepquantum.mbqc.pattern module
- deepquantum.mbqc.state module- SubGraphState- SubGraphState.to()
- SubGraphState.nodes
- SubGraphState.edges
- SubGraphState.full_state
- SubGraphState.set_graph()
- SubGraphState.set_state()
- SubGraphState.set_nodes_out_seq()
- SubGraphState.add_nodes()
- SubGraphState.add_edges()
- SubGraphState.shift_labels()
- SubGraphState.compose()
- SubGraphState.update_node2wire_dict()
- SubGraphState.draw()
 
- GraphState
 
- Module contents
 
- deepquantum.photonic package- Submodules
- deepquantum.photonic.ansatz module
- deepquantum.photonic.channel module
- deepquantum.photonic.circuit module- QumodeCircuit- QumodeCircuit.set_init_state()
- QumodeCircuit.to()
- QumodeCircuit.forward()
- QumodeCircuit.global_circuit()
- QumodeCircuit.encode()
- QumodeCircuit.get_unitary()
- QumodeCircuit.get_symplectic()
- QumodeCircuit.get_displacement()
- QumodeCircuit.get_amplitude()
- QumodeCircuit.get_prob()
- QumodeCircuit.measure()
- QumodeCircuit.photon_number_mean_var()
- QumodeCircuit.measure_homodyne()
- QumodeCircuit.max_depth
- QumodeCircuit.draw()
- QumodeCircuit.cat()
- QumodeCircuit.gkp()
- QumodeCircuit.add()
- QumodeCircuit.ps()
- QumodeCircuit.bs()
- QumodeCircuit.mzi()
- QumodeCircuit.bs_theta()
- QumodeCircuit.bs_phi()
- QumodeCircuit.bs_rx()
- QumodeCircuit.bs_ry()
- QumodeCircuit.bs_h()
- QumodeCircuit.dc()
- QumodeCircuit.h()
- QumodeCircuit.any()
- QumodeCircuit.clements()
- QumodeCircuit.s()
- QumodeCircuit.s2()
- QumodeCircuit.d()
- QumodeCircuit.x()
- QumodeCircuit.z()
- QumodeCircuit.r()
- QumodeCircuit.f()
- QumodeCircuit.qp()
- QumodeCircuit.cx()
- QumodeCircuit.cz()
- QumodeCircuit.cp()
- QumodeCircuit.k()
- QumodeCircuit.ck()
- QumodeCircuit.delay()
- QumodeCircuit.homodyne()
- QumodeCircuit.homodyne_x()
- QumodeCircuit.homodyne_p()
- QumodeCircuit.loss()
- QumodeCircuit.loss_t()
- QumodeCircuit.loss_db()
- QumodeCircuit.barrier()
 
- DistributedQumodeCircuit
 
- deepquantum.photonic.decompose module
- deepquantum.photonic.distributed module
- deepquantum.photonic.draw module
- deepquantum.photonic.gate module
- deepquantum.photonic.hafnian_ module
- deepquantum.photonic.mapper module- UnitaryMapper- UnitaryMapper.create_basis()
- UnitaryMapper.get_coeff_sym()
- UnitaryMapper.get_indx_coeff()
- UnitaryMapper.indx_eqs()
- UnitaryMapper.single_prod()
- UnitaryMapper.single_output()
- UnitaryMapper.get_transfer_mat()
- UnitaryMapper.f_real()
- UnitaryMapper.f_real_unitary()
- UnitaryMapper.unitary_constrains()
- UnitaryMapper.f_complex_unitary()
- UnitaryMapper.f_complex()
- UnitaryMapper.unitary_constrains_complex()
- UnitaryMapper.solve_eqs_real()
- UnitaryMapper.solve_eqs_complex()
- UnitaryMapper.exchange()
- UnitaryMapper.sub_matrix_sym()
- UnitaryMapper.set_copy_indx()
- UnitaryMapper.permanent()
- UnitaryMapper.product_factorial()
- UnitaryMapper.create_subset()
- UnitaryMapper.save_dict()
- UnitaryMapper.plot_u()
- UnitaryMapper.is_unitary()
- UnitaryMapper.state_basis()
 
 
- deepquantum.photonic.measurement module
- deepquantum.photonic.operation module
- deepquantum.photonic.qmath module- dirac_ket()
- sort_dict_fock_basis()
- sub_matrix()
- permanent()
- create_subset()
- get_powerset()
- permanent_ryser()
- product_factorial()
- fock_combinations()
- ladder_ops()
- shift_func()
- xxpp_to_xpxp()
- xpxp_to_xxpp()
- quadrature_to_ladder()
- ladder_to_quadrature()
- photon_number_mean_var()
- takagi()
- sqrtm_herm()
- schur_anti_symm_even()
- williamson()
- measure_fock_tensor()
- sample_homodyne_fock()
- sample_reject_bosonic()
- align_shape()
 
- deepquantum.photonic.state module
- deepquantum.photonic.tdm module
- deepquantum.photonic.torontonian_ module
- deepquantum.photonic.utils module
- Module contents
 
Submodules#
deepquantum.adjoint module#
Adjoint differentiation
- class deepquantum.adjoint.AdjointExpectation(*args, **kwargs)#
- 基类: - Function- Adjoint differentiation - See https://arxiv.org/pdf/2009.02823 - 参数:
- state (DistributedQubitState) – The final quantum state. 
- operators (nn.Sequential) – The quantum operations. 
- observable (Observable) – The observable. 
- *parameters (torch.Tensor) – The parameters of the quantum circuit. 
 
 - static forward(ctx, state: DistributedQubitState, operators: Sequential, observable: Observable, *parameters: Tensor) Tensor#
- Define the forward of the custom autograd Function. - This function is to be overridden by all subclasses. There are two ways to define forward: - Usage 1 (Combined forward and ctx): - @staticmethod def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any: pass - It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types). 
- See combining-forward-context for more details 
 - Usage 2 (Separate forward and ctx): - @staticmethod def forward(*args: Any, **kwargs: Any) -> Any: pass @staticmethod def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None: pass - The forward no longer accepts a ctx argument. 
- Instead, you must also override the - torch.autograd.Function.setup_context()staticmethod to handle setting up the- ctxobject.- outputis the output of the forward,- inputsare a Tuple of inputs to the forward.
- See extending-autograd for more details 
 - The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with - ctx.save_for_backward()if they are intended to be used in- backward(equivalently,- vjp) or- ctx.save_for_forward()if they are intended to be used for in- jvp.
 - static backward(ctx, grad_out: Tensor) Tuple[None, ...]#
- Define a formula for differentiating the operation with backward mode automatic differentiation. - This function is to be overridden by all subclasses. (Defining this function is equivalent to defining the - vjpfunction.)- It must accept a context - ctxas the first argument, followed by as many outputs as the- forward()returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs to- forward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.- The context can be used to retrieve tensors saved during the forward pass. It also has an attribute - ctx.needs_input_gradas a tuple of booleans representing whether each input needs gradient. E.g.,- backward()will have- ctx.needs_input_grad[0] = Trueif the first input to- forward()needs gradient computed w.r.t. the output.
 
deepquantum.ansatz module#
Ansatze: various quantum circuits
- class deepquantum.ansatz.Ansatz(nqubit: int, wires: int | List[int] | None = None, minmax: List[int] | None = None, ancilla: int | List[int] | None = None, controls: int | List[int] | None = None, init_state: Any = 'zeros', name: str | None = None, den_mat: bool = False, reupload: bool = False, mps: bool = False, chi: int | None = None)#
- 基类: - QubitCircuit- A base class for Ansatz. - 参数:
- nqubit (int) – The number of qubits in the circuit. 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Only valid when - wiresis- None. Default:- None
- ancilla (int, List[int] or None, optional) – The indices of the ancilla qubits. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- init_state (Any, optional) – The initial state of the circuit. Default: - 'zeros'
- name (str or None, optional) – The name of the circuit. Default: - None
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- reupload (bool, optional) – Whether to use data re-uploading. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
 
 
- class deepquantum.ansatz.ControlledMultiplier(nqubit: int, a: int, mod: int, minmax: List[int] | None = None, nqubitx: int | None = None, ancilla: int | List[int] | None = None, controls: int | List[int] | None = None, den_mat: bool = False, mps: bool = False, chi: int | None = None, debug: bool = False)#
- 基类: - Ansatz- Controlled multiplier. - See https://arxiv.org/pdf/quant-ph/0205095.pdf Fig.6 - 参数:
- nqubit (int) – The number of qubits in the circuit. 
- a (int) – Number - ain \(b+a*x \mod N\).
- mod (int) – The modulus in \(b+a*x \mod N\). 
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: - None
- ancilla (int, List[int] or None, optional) – The indices of the ancilla qubits. Default: - None
- nqubitx (int or None, optional) – The number of qubits in the register x. 
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
- debug (bool, optional) – Whether to print the debug information. Default: - False
 
 
- class deepquantum.ansatz.ControlledUa(nqubit: int, a: int, mod: int, minmax: List[int] | None = None, ancilla: int | List[int] | None = None, controls: int | List[int] | None = None, den_mat: bool = False, mps: bool = False, chi: int | None = None, debug: bool = False)#
- 基类: - Ansatz- Controlled Ua gate. - ahas a modular inverse only if- ais coprime to- mod. See https://arxiv.org/pdf/quant-ph/0205095.pdf Fig.7- 参数:
- nqubit (int) – The number of qubits in the circuit. 
- a (int) – Number - ain \(a*x \mod N\).
- mod (int) – The modulus in \(a*x \mod N\). 
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: - None
- ancilla (int, List[int] or None, optional) – The indices of the ancilla qubits. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
- debug (bool, optional) – Whether to print the debug information. Default: - False
 
 
- class deepquantum.ansatz.HHL(ncount: int, mat: Any, t0: float = 1, den_mat: bool = False, mps: bool = False, chi: int | None = None, show_barrier: bool = False)#
- 基类: - Ansatz- A quantum circuit for the HHL algorithm. - 参数:
- ncount (int) – The number of counting qubits. 
- mat (Any) – The Hermitian matrix A. 
- t0 (float, optional) – The time parameter for the matrix exponential in units of \(2\pi\). Default: 1 
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
- show_barrier (bool, optional) – Whether to show the barriers in the circuit. Default: - False
 
 
- class deepquantum.ansatz.NumberEncoder(nqubit: int, number: int, minmax: List[int] | None = None, den_mat: bool = False, mps: bool = False, chi: int | None = None)#
- 基类: - Ansatz- Convert number to corresponding encoding circuit. - 参数:
- nqubit (int) – The number of qubits in the circuit. 
- number (int) – The integer for converting to bit string. 
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
 
 
- class deepquantum.ansatz.PhiAdder(nqubit: int, number: int, minmax: List[int] | None = None, controls: int | List[int] | None = None, den_mat: bool = False, mps: bool = False, chi: int | None = None, debug: bool = False)#
- 基类: - Ansatz- Phi adder. - See https://arxiv.org/pdf/quant-ph/0205095.pdf Fig.2 and Fig.3 - 参数:
- nqubit (int) – The number of qubits in the circuit. 
- number (int) – Number - ain \(\Phi(a+b)\).
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
- debug (bool, optional) – Whether to print the debug information. Default: - False
 
 
- class deepquantum.ansatz.PhiModularAdder(nqubit: int, number: int, mod: int, minmax: List[int] | None = None, ancilla: int | List[int] | None = None, controls: int | List[int] | None = None, den_mat: bool = False, mps: bool = False, chi: int | None = None, debug: bool = False)#
- 基类: - Ansatz- Phi modular adder. - See https://arxiv.org/pdf/quant-ph/0205095.pdf Fig.5 - 参数:
- nqubit (int) – The number of qubits in the circuit. 
- number (int) – Number - ain \(\Phi(a+b \mod N)\).
- mod (int) – The modulus in \(\Phi(a+b \mod N)\). 
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: - None
- ancilla (int, List[int] or None, optional) – The indices of the ancilla qubits. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
- debug (bool, optional) – Whether to print the debug information. Default: - False
 
 
- class deepquantum.ansatz.QuantumConvolutionalNeuralNetwork(nqubit: int, nlayer: int, minmax: List[int] | None = None, init_state: Any = 'zeros', den_mat: bool = False, requires_grad: bool = True, mps: bool = False, chi: int | None = None)#
- 基类: - Ansatz- Quantum convolutional neural network. - See https://readpaper.com/paper/4554418257818296321 Fig.1 or https://pennylane.ai/qml/demos/tutorial_learning_few_data - 参数:
- nqubit (int) – The number of qubits in the circuit. 
- nlayer (int) – The number of layers. 
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: - None
- init_state (Any, optional) – The initial state of the circuit. Default: - 'zeros'
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- requires_grad (bool, optional) – Whether the parameters are - nn.Parameteror- buffer. Default:- True(which means- nn.Parameter)
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
 
 - conv(wires: List[int]) None#
 - pool(wires: List[int]) None#
 
- class deepquantum.ansatz.QuantumFourierTransform(nqubit: int, minmax: List[int] | None = None, reverse: bool = False, init_state: Any = 'zeros', den_mat: bool = False, mps: bool = False, chi: int | None = None, show_barrier: bool = False)#
- 基类: - Ansatz- Quantum Fourier transform. - 参数:
- nqubit (int) – The number of qubits in the circuit. 
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: - None
- reverse (bool, optional) – Whether to reverse the output order. Default: - False(which means the default output order of phase is \(x/2, ..., x/2^n\). If- reverse=True, the output order of phase is \(x/2^n, ..., x/2\))
- init_state (Any, optional) – The initial state of the circuit. Default: - 'zeros'
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
- show_barrier (bool, optional) – Whether to show the barriers in the circuit. Default: - False
 
 - qft_block(n: int) None#
 
- class deepquantum.ansatz.QuantumPhaseEstimation(nqubit: int, ncount: int, unitary: Any, minmax: List[int] | None = None, den_mat: bool = False, mps: bool = False, chi: int | None = None, show_barrier: bool = False)#
- 基类: - Ansatz- Quantum phase estimation for arbitrary unitary operator. - 参数:
- nqubit (int) – The number of qubits in the circuit. 
- ncount (int) – The number of counting qubits. 
- unitary (Any) – The unitary operator. 
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
- show_barrier (bool, optional) – Whether to show the barriers in the circuit. Default: - False
 
 
- class deepquantum.ansatz.QuantumPhaseEstimationSingleQubit(t: int, phase: Any, den_mat: bool = False, mps: bool = False, chi: int | None = None)#
- 基类: - Ansatz- Quantum phase estimation for single-qubit gate. - 参数:
- t (int) – The number of counting qubits. 
- phase (Any) – The phase to be estimated. 
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
 
 
- class deepquantum.ansatz.RandomCircuitG3(nqubit: int, ngate: int, wires: List[int] | None = None, minmax: List[int] | None = None, init_state: Any = 'zeros', den_mat: bool = False, mps: bool = False, chi: int | None = None)#
- 基类: - Ansatz- Random circuit of G3 family. - 参数:
- nqubit (int) – The number of qubits in the circuit. 
- ngate (int) – The number of random gates in the circuit. 
- wires (List[int] or None, optional) – The indices of the qubits that the random gates act on. Default: - None
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Only valid when - wiresis- None. Default:- None
- init_state (Any, optional) – The initial state of the circuit. Default: - 'zeros'
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
 
 
- class deepquantum.ansatz.ShorCircuit(mod: int, ncount: int, a: int, den_mat: bool = False, mps: bool = False, chi: int | None = None, debug: bool = False)#
- 基类: - Ansatz- Circuit for Shor’s algorithm. - 参数:
- mod (int) – The odd integer to be factored. 
- ncount (int) – The number of counting qubits. 
- a (int) – Any integer that satisfies \(1 < a < N\) and \(\gcd(a, N) = 1\). 
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
- debug (bool, optional) – Whether to print the debug information. Default: - False
 
 
- class deepquantum.ansatz.ShorCircuitFor15(ncount: int, a: int, den_mat: bool = False, mps: bool = False, chi: int | None = None)#
- 基类: - Ansatz- Circuit for Shor’s algorithm to factor number 15. - See https://learn.qiskit.org/course/ch-algorithms/shors-algorithm - 参数:
- ncount (int) – The number of counting qubits. 
- a (int) – Any integer that satisfies \(1 < a < N\) and \(\gcd(a, N) = 1\). 
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
 
 - cua(a: int, power: int, controls: int | List[int] | None) None#
 
deepquantum.bitmath module#
Bit-twiddling functions of unsigned integers
- deepquantum.bitmath.power_of_2(exp: int) int#
- deepquantum.bitmath.is_power_of_2(number: int) bool#
- deepquantum.bitmath.log_base2(number: int) int#
- deepquantum.bitmath.get_bit(number: int | Tensor, bit_index: int) int | Tensor#
- deepquantum.bitmath.flip_bit(number: int | Tensor, bit_index: int) int | Tensor#
- deepquantum.bitmath.flip_bits(number: int | Tensor, bit_indices: List[int]) int | Tensor#
- deepquantum.bitmath.insert_bit(number: int | Tensor, bit_index: int, bit_value: int) int | Tensor#
- deepquantum.bitmath.all_bits_are_one(number: int, bit_indices: List[int]) bool#
- deepquantum.bitmath.get_bit_mask(bit_indices: List[int]) int#
deepquantum.channel module#
Quantum channels
- class deepquantum.channel.BitFlip(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, tsr_mode: bool = False, requires_grad: bool = False)#
- 基类: - Channel- Bit flip channel. - The bit flip channel is defined as: \[\rho \Rightarrow (1-p) \rho + p X \rho X^{\dagger}\]- 参数:
- inputs (Any, optional) – The parameter of the channel. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local Kraus matrices acting on density matrices. 
 
- class deepquantum.channel.PhaseFlip(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, tsr_mode: bool = False, requires_grad: bool = False)#
- 基类: - Channel- Phase flip channel. - The phase flip channel is defined as: \[\rho \Rightarrow (1-p) \rho + p Z \rho Z^{\dagger}\]- 参数:
- inputs (Any, optional) – The parameter of the channel. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local Kraus matrices acting on density matrices. 
 
- class deepquantum.channel.Depolarizing(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, tsr_mode: bool = False, requires_grad: bool = False)#
- 基类: - Channel- Depolarizing channel. - The depolarizing channel is defined as: \[\rho \Rightarrow (1-p) \rho + p/3 X \rho X^{\dagger} + p/3 Y \rho Y^{\dagger} + p/3 Z \rho Z^{\dagger}\]- 参数:
- inputs (Any, optional) – The parameter of the channel. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local Kraus matrices acting on density matrices. 
 
- class deepquantum.channel.Pauli(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, tsr_mode: bool = False, requires_grad: bool = False)#
- 基类: - Channel- Pauli channel. - The Pauli channel is defined as: \[\rho \Rightarrow (1-px-py-pz) \rho + px X \rho X^{\dagger} + py Y \rho Y^{\dagger} + pz Z \rho Z^{\dagger}\]- 参数:
- inputs (Any, optional) – The parameters of the channel. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - property prob#
- The error probabilities. 
 - inputs_to_tensor(inputs: Any | None = None) Tensor#
- Convert inputs to torch.Tensor. 
 - get_matrix(theta: Any) Tensor#
- Get the local Kraus matrices acting on density matrices. 
 
- class deepquantum.channel.AmplitudeDamping(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, tsr_mode: bool = False, requires_grad: bool = False)#
- 基类: - Channel- Amplitude damping channel. - The amplitude damping channel is defined as: \[\begin{split}\rho \Rightarrow K_0 \rho K_0^{\dagger} + K_1 \rho K_1^{\dagger}, K_0 = \begin{pmatrix} 1 & 0 \\ 0 & \sqrt{1 - p} \end{pmatrix}, K_1 = \begin{pmatrix} 0 & \sqrt{p} \\ 0 & 0 \end{pmatrix}\end{split}\]- 参数:
- inputs (Any, optional) – The parameter of the channel. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local Kraus matrices acting on density matrices. 
 
- class deepquantum.channel.PhaseDamping(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, tsr_mode: bool = False, requires_grad: bool = False)#
- 基类: - Channel- Phase damping channel. - The phase damping channel is defined as: \[\begin{split}\rho \Rightarrow K_0 \rho K_0^{\dagger} + K_1 \rho K_1^{\dagger}, K_0 = \begin{pmatrix} 1 & 0 \\ 0 & \sqrt{1 - p} \end{pmatrix}, K_1 = \begin{pmatrix} 0 & 0 \\ 0 & \sqrt{p} \end{pmatrix}\end{split}\]- 参数:
- inputs (Any, optional) – The parameter of the channel. The Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local Kraus matrices acting on density matrices. 
 
- class deepquantum.channel.GeneralizedAmplitudeDamping(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, tsr_mode: bool = False, requires_grad: bool = False)#
- 基类: - Channel- Generalized amplitude damping channel. - The generalized amplitude damping channel is defined as: \[\begin{split}\rho \Rightarrow K_0 \rho K_0^{\dagger} + K_1 \rho K_1^{\dagger} + K_2 \rho K_2^{\dagger} + K_3 \rho K_3^{\dagger}, K_0 = \sqrt{p} \begin{pmatrix} 1 & 0 \\ 0 & \sqrt{1 - \gamma} \end{pmatrix}, K_1 = \sqrt{p} \begin{pmatrix} 0 & \sqrt{\gamma} \\ 0 & 0 \end{pmatrix}, K_2 = \sqrt{1 - p} \begin{pmatrix} \sqrt{1 - \gamma} & 0 \\ 0 & 1 \end{pmatrix}, K_3 = \sqrt{1 - p} \begin{pmatrix} 0 & 0 \\ \sqrt{\gamma} & 0 \end{pmatrix}\end{split}\]- 参数:
- inputs (Any, optional) – The parameters of the channel. The first parameter is the probability of amplitude damping error, and the second parameter is the damping rate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - inputs_to_tensor(inputs: Any | None = None) Tensor#
- Convert inputs to torch.Tensor. 
 - get_matrix(theta: Any) Tensor#
- Get the local Kraus matrices acting on density matrices. 
 
deepquantum.circuit module#
Quantum circuit
- class deepquantum.circuit.QubitCircuit(nqubit: int, init_state: Any = 'zeros', name: str | None = None, den_mat: bool = False, reupload: bool = False, mps: bool = False, chi: int | None = None, shots: int = 1024)#
- 基类: - Operation- Quantum circuit for qubits. - This class inherits from the - Operationclass and implements methods for creating, manipulating, and measuring quantum states.- 参数:
- nqubit (int) – The number of qubits in the circuit. 
- init_state (Any, optional) – The initial state of the circuit. Default: - 'zeros'
- name (str or None, optional) – The name of the circuit. Default: - None
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- reupload (bool, optional) – Whether to use data re-uploading. Default: - False
- mps (bool, optional) – Whether to use matrix product state representation. Default: - False
- chi (int or None, optional) – The bond dimension for matrix product state representation. Default: - None
- shots (int, optional) – The number of shots for the measurement. Default: 1024 
 
- 抛出:
- AssertionError – If the type or dimension of - init_statedoes not match- nqubitor- den_mat.
 - set_init_state(init_state: Any) None#
- Set the initial state of the circuit. 
 - to(arg: Any) QubitCircuit#
- Set dtype or device of the - QubitCircuit.
 - forward(data: Tensor | None = None, state: Tensor | QubitState | List[Tensor] | MatrixProductState | None = None) Tensor | List[Tensor]#
- Perform a forward pass of the quantum circuit and return the final state. - This method applies the - operatorsof the quantum circuit to the initial state or the given state and returns the resulting state. If- datais given, it is used as the input for the- encoders. The- statecan be either a- MatrixProductStateor a- QubitStateobject, or a tensor representation of them. The- datamust be a 1D or 2D tensor.- 参数:
- data (torch.Tensor or None, optional) – The input data for the - encoders. Default:- None
- state (torch.Tensor, QubitState, List[torch.Tensor], MatrixProductState or None, optional) – The initial state for the quantum circuit. Default: - None
 
- 返回:
- The final state of the quantum circuit after applying the - operators.
- 返回类型:
- Union[torch.Tensor, List[torch.Tensor]] 
 
 - encode(data: Tensor | None) None#
- Encode the input data into the quantum circuit parameters. - This method iterates over the - encodersof the quantum circuit and initializes their parameters with the input data. If- reuploadis- False, the input data must be at least as long as the number of parameters in the- encoders. If- reuploadis- True, the input data can be repeated to fill up the parameters.- 参数:
- data (torch.Tensor or None) – The input data for the - encoders, must be a 1D tensor.
- 抛出:
- AssertionError – If - reuploadis- Falseand the input data is shorter than the number of parameters in the- encoders.
 
 - init_para() None#
- Initialize the parameters of the - operators.
 - init_encoder() None#
- Initialize the parameters of the - encoders.
 - reset_circuit(init_state: Any = 'zeros') None#
- Reset the - QubitCircuitaccording to- init_state.
 - amplitude_encoding(data: Any) Tensor#
- Encode data into quantum states using amplitude encoding. 
 - observable(wires: int | List[int] | None = None, basis: str = 'z') None#
- Add an - Observable.- 参数:
- wires (int, List[int] or None, optional) – The wires to measure. Default: - None(which means all wires are measured)
- basis (str, optional) – The measurement basis for each wire. It can be - 'x',- 'y', or- 'z'. If only one character is given, it is repeated for all wires. Default:- 'z'
 
 
 - reset_observable() None#
- Reset the - observables.
 - measure(shots: int | None = None, with_prob: bool = False, wires: int | List[int] | None = None, block_size: int = 16777216) Dict | List[Dict] | None#
- Measure the final state. - 参数:
- shots (int or None, optional) – The number of shots for the measurement. Default: - None(which means- self.shots)
- with_prob (bool, optional) – Whether to show the true probability of the measurement. Default: - False
- wires (int, List[int] or None, optional) – The wires to measure. Default: - None(which means all wires)
- block_size (int, optional) – The block size for sampling. Default: 2 ** 24 
 
 
 - expectation(shots: int | None = None) Tensor#
- Get the expectation value according to the final state and - observables.- 参数:
- shots (int or None, optional) – The number of shots for the expectation value. Default: - None(which means the exact and differentiable expectation value).
 
 - defer_measure(with_prob: bool = False) Tensor | Tuple[Tensor, List, List]#
- Get the state vectors and the measurement results after deferred measurement. 
 - post_select(bits: str) Tensor#
- Get the state vectors after post selection. 
 - get_unitary() Tensor#
- Get the unitary matrix of the quantum circuit. 
 - get_amplitude(bits: str) Tensor#
- Get the amplitude for the given bit string. - 参数:
- bits (str) – A bit string. 
 
 - get_prob(bits: str, wires: int | List[int] | None = None) Tensor#
- Get the probability for the given bit string. - 参数:
- bits (str) – A bit string. 
- wires (int, List[int] or None, optional) – The wires to measure. It can be an integer or a list of integers specifying the indices of the wires. 
 
 
 - inverse(encode: bool = False) QubitCircuit#
- Get the inversed circuit. - 备注 - The inversed circuit shares the parameters with the original circuit. You should ONLY encode data onto the original circuit. If you want to encode data onto the inversed circuit, set - encodeto be- True.
 - property max_depth: int#
- Get the max number of gates on the wires. 
 - qasm() str#
- Get QASM of the quantum circuit. 
 - transform_cut2move() QubitCircuit#
- Transform - WireCutto- Moveand expand the observables accordingly.
 - get_subexperiments(qubit_labels: Sequence[Hashable] | None = None) Tuple[Dict, List[float]]#
- Generate cutting subexperiments and their associated coefficients. 
 - draw(output: str = 'mpl', **kwargs)#
- Visualize the quantum circuit. 
 - add(op: Operation, encode: bool = False, wires: int | List[int] | None = None, controls: int | List[int] | None = None) None#
- A method that adds an operation to the quantum circuit. - The operation can be a gate, a layer, or another quantum circuit. The method also updates the attributes of the quantum circuit. If - wiresis specified, the parameters of gates are shared.- 参数:
- op (Operation) – The operation to add. It is an instance of - Operationclass or its subclasses, such as- Gate,- Layer,- Channel, or- QubitCircuit.
- encode (bool, optional) – Whether the gate or layer is to encode data. Default: - False
- wires (int, List[int] or None, optional) – The wires to apply the gate on. It can be an integer or a list of integers specifying the indices of the wires. Default: - None(which means the gate has its own wires)
- controls (int, List[int] or None, optional) – The control wires for the gate. It can be an integer or a list of integers specifying the indices of the control wires. Only valid when - wiresis not- None. Default:- None(which means the gate has its own control wires)
 
- 抛出:
- AssertionError – If the input arguments are invalid or incompatible with the quantum circuit. 
 
 - u3(wires: int, inputs: Any | None = None, controls: int | List[int] | None = None, condition: bool = False, encode: bool = False) None#
- Add a U3 gate. 
 - cu(control: int, target: int, inputs: Any | None = None, encode: bool = False) None#
- Add a controlled U3 gate. 
 - p(wires: int, inputs: Any | None = None, controls: int | List[int] | None = None, condition: bool = False, encode: bool = False) None#
- Add a phase shift gate. 
 - cp(control: int, target: int, inputs: Any | None = None, encode: bool = False) None#
- Add a controlled phase shift gate. 
 - x(wires: int, controls: int | List[int] | None = None, condition: bool = False) None#
- Add a Pauli-X gate. 
 - y(wires: int, controls: int | List[int] | None = None, condition: bool = False) None#
- Add a Pauli-Y gate. 
 - z(wires: int, controls: int | List[int] | None = None, condition: bool = False) None#
- Add a Pauli-Z gate. 
 - h(wires: int, controls: int | List[int] | None = None, condition: bool = False) None#
- Add a Hadamard gate. 
 - s(wires: int, controls: int | List[int] | None = None, condition: bool = False) None#
- Add an S gate. 
 - sdg(wires: int, controls: int | List[int] | None = None, condition: bool = False) None#
- Add an S dagger gate. 
 - t(wires: int, controls: int | List[int] | None = None, condition: bool = False) None#
- Add a T gate. 
 - tdg(wires: int, controls: int | List[int] | None = None, condition: bool = False) None#
- Add a T dagger gate. 
 - ch(control: int, target: int) None#
- Add a controlled Hadamard gate. 
 - cs(control: int, target: int) None#
- Add a controlled S gate. 
 - csdg(control: int, target: int) None#
- Add a controlled S dagger gate. 
 - ct(control: int, target: int) None#
- Add a controlled T gate. 
 - ctdg(control: int, target: int) None#
- Add a controlled T dagger gate. 
 - rx(wires: int, inputs: Any | None = None, controls: int | List[int] | None = None, condition: bool = False, encode: bool = False) None#
- Add an Rx gate. 
 - ry(wires: int, inputs: Any | None = None, controls: int | List[int] | None = None, condition: bool = False, encode: bool = False) None#
- Add an Ry gate. 
 - rz(wires: int, inputs: Any | None = None, controls: int | List[int] | None = None, condition: bool = False, encode: bool = False) None#
- Add an Rz gate. 
 - crx(control: int, target: int, inputs: Any | None = None, encode: bool = False) None#
- Add a controlled Rx gate. 
 - cry(control: int, target: int, inputs: Any | None = None, encode: bool = False) None#
- Add a controlled Ry gate. 
 - crz(control: int, target: int, inputs: Any | None = None, encode: bool = False) None#
- Add a controlled Rz gate. 
 - j(wires: int, inputs: Any | None = None, plane: str = 'xy', controls: int | List[int] | None = None, condition: bool = False, encode: bool = False) None#
- Add a projection matrix J. 
 - cnot(control: int, target: int) None#
- Add a CNOT gate. 
 - cx(control: int, target: int) None#
- Add a CNOT gate. 
 - cy(control: int, target: int) None#
- Add a controlled Y gate. 
 - cz(control: int, target: int) None#
- Add a controlled Z gate. 
 - swap(wires: List[int], controls: int | List[int] | None = None, condition: bool = False) None#
- Add a SWAP gate. 
 - rxx(wires: List[int], inputs: Any | None = None, controls: int | List[int] | None = None, condition: bool = False, encode: bool = False) None#
- Add an Rxx gate. 
 - ryy(wires: List[int], inputs: Any | None = None, controls: int | List[int] | None = None, condition: bool = False, encode: bool = False) None#
- Add an Ryy gate. 
 - rzz(wires: List[int], inputs: Any | None = None, controls: int | List[int] | None = None, condition: bool = False, encode: bool = False) None#
- Add an Rzz gate. 
 - rxy(wires: List[int], inputs: Any | None = None, controls: int | List[int] | None = None, condition: bool = False, encode: bool = False) None#
- Add an Rxy gate. 
 - rbs(wires: List[int], inputs: Any | None = None, controls: int | List[int] | None = None, condition: bool = False, encode: bool = False) None#
- Add a Reconfigurable Beam Splitter gate. 
 - crxx(control: int, target1: int, target2: int, inputs: Any | None = None, encode: bool = False) None#
- Add a controlled Rxx gate. 
 - cryy(control: int, target1: int, target2: int, inputs: Any | None = None, encode: bool = False) None#
- Add a controlled Ryy gate. 
 - crzz(control: int, target1: int, target2: int, inputs: Any | None = None, encode: bool = False) None#
- Add a controlled Rzz gate. 
 - crxy(control: int, target1: int, target2: int, inputs: Any | None = None, encode: bool = False) None#
- Add a controlled Rxy gate. 
 - toffoli(control1: int, control2: int, target: int) None#
- Add a Toffoli gate. 
 - ccx(control1: int, control2: int, target: int) None#
- Add a Toffoli gate. 
 - fredkin(control: int, target1: int, target2: int) None#
- Add a Fredkin gate. 
 - cswap(control: int, target1: int, target2: int) None#
- Add a Fredkin gate. 
 - any(unitary: Any, wires: int | List[int] | None = None, minmax: List[int] | None = None, controls: int | List[int] | None = None, name: str = 'uany') None#
- Add an arbitrary unitary gate. 
 - latent(wires: int | List[int] | None = None, minmax: List[int] | None = None, inputs: Any | None = None, controls: int | List[int] | None = None, encode: bool = False, name: str = 'latent') None#
- Add a latent gate. 
 - hamiltonian(hamiltonian: Any, t: Any | None = None, wires: int | List[int] | None = None, minmax: List[int] | None = None, controls: int | List[int] | None = None, encode: bool = False, name: str = 'hamiltonian') None#
- Add a Hamiltonian gate. 
 - xlayer(wires: int | List[int] | None = None) None#
- Add a layer of Pauli-X gates. 
 - ylayer(wires: int | List[int] | None = None) None#
- Add a layer of Pauli-Y gates. 
 - zlayer(wires: int | List[int] | None = None) None#
- Add a layer of Pauli-Z gates. 
 - hlayer(wires: int | List[int] | None = None) None#
- Add a layer of Hadamard gates. 
 - rxlayer(wires: int | List[int] | None = None, inputs: Any | None = None, encode: bool = False) None#
- Add a layer of Rx gates. 
 - rylayer(wires: int | List[int] | None = None, inputs: Any | None = None, encode: bool = False) None#
- Add a layer of Ry gates. 
 - rzlayer(wires: int | List[int] | None = None, inputs: Any | None = None, encode: bool = False) None#
- Add a layer of Rz gates. 
 - u3layer(wires: int | List[int] | None = None, inputs: Any | None = None, encode: bool = False) None#
- Add a layer of U3 gates. 
 - cxlayer(wires: List[List[int]] | None = None) None#
- Add a layer of CNOT gates. 
 - cnot_ring(minmax: List[int] | None = None, step: int = 1, reverse: bool = False) None#
- Add a layer of CNOT gates in a cyclic way. 
 - bit_flip(wires: int, inputs: Any | None = None, encode: bool = False) None#
- Add a bit-flip channel. 
 - phase_flip(wires: int, inputs: Any | None = None, encode: bool = False) None#
- Add a phase-flip channel. 
 - depolarizing(wires: int, inputs: Any | None = None, encode: bool = False) None#
- Add a depolarizing channel. 
 - pauli(wires: int, inputs: Any | None = None, encode: bool = False) None#
- Add a Pauli channel. 
 - amp_damp(wires: int, inputs: Any | None = None, encode: bool = False) None#
- Add an amplitude-damping channel. 
 - phase_damp(wires: int, inputs: Any | None = None, encode: bool = False) None#
- Add a phase-damping channel. 
 - gen_amp_damp(wires: int, inputs: Any | None = None, encode: bool = False) None#
- Add a generalized amplitude-damping channel. 
 - reset(wires: int | List[int] | None = None, postselect: int | None = 0) None#
- Add a reset operation. 
 - barrier(wires: int | List[int] | None = None) None#
- Add a barrier. 
 - cut(wires: int | List[int]) None#
- Add a wire-cut operation. 
 - move(wire1: int, wire2: int, postselect: int | None = 0) None#
- Add a move operation. 
 
- class deepquantum.circuit.DistributedQubitCircuit(nqubit, name=None, reupload=False, shots=1024)#
- 基类: - QubitCircuit- Quantum circuit for a distributed state vector. - 参数:
- nqubit (int) – The number of qubits in the circuit. 
- name (str or None, optional) – The name of the circuit. Default: - None
- reupload (bool, optional) – Whether to use data re-uploading. Default: - False
- shots (int, optional) – The number of shots for the measurement. Default: 1024 
 
 - set_init_state(init_state: str | DistributedQubitState = 'zeros') None#
- Set the initial state of the circuit. 
 - forward(data: Tensor | None = None, state: DistributedQubitState | None = None) DistributedQubitState#
- Perform a forward pass of the quantum circuit and return the final state. - This method applies the - operatorsof the quantum circuit to the initial state or the given state and returns the resulting state. If- datais given, it is used as the input for the- encoders. The- datamust be a 1D tensor.- 参数:
- data (torch.Tensor or None, optional) – The input data for the - encoders. Default:- None
- state (DistributedQubitState or None, optional) – The initial state for the quantum circuit. Default: - None
 
 
 - measure(shots: int | None = None, with_prob: bool = False, wires: int | List[int] | None = None, block_size: int = 16777216) Dict | None#
- Measure the final state. - 参数:
- shots (int or None, optional) – The number of shots for the measurement. Default: - None(which means- self.shots)
- with_prob (bool, optional) – Whether to show the true probability of the measurement. Default: - False
- wires (int, List[int] or None, optional) – The wires to measure. Default: - None(which means all wires)
- block_size (int, optional) – The block size for sampling. Default: 2 ** 24 
 
 
 - expectation(shots: int | None = None) Tensor#
- Get the expectation value according to the final state and - observables.- 参数:
- shots (int or None, optional) – The number of shots for the expectation value. Default: - None(which means the exact and differentiable expectation value).
 
 - cnot(control: int, target: int) None#
- Add a CNOT gate. 
 - toffoli(control1: int, control2: int, target: int) None#
- Add a Toffoli gate. 
 
deepquantum.communication module#
Communication utilities
- deepquantum.communication.setup_distributed(backend: str = 'nccl', port: str = '29500') Tuple[int, int, int]#
- Initialize torch.distributed. 
- deepquantum.communication.cleanup_distributed() None#
- Clean up the distributed environment. 
- deepquantum.communication.comm_get_rank() int#
- deepquantum.communication.comm_get_world_size() int#
- deepquantum.communication.comm_exchange_arrays(send_data: Tensor, recv_data: Tensor, pair_rank: int | None) None#
- Simulate a point-to-point exchange using dist.all_to_all_single with output_split_sizes and input_split_sizes to minimize memory. If pair_rank is None, this rank participates in the collective call but sends/receives no actual data to/from other specific ranks in this logical P2P. - 参数:
- send_data (torch.Tensor) – The data this rank wants to send to pair_rank. If pair_rank is None, this can be an empty tensor with correct dtype and device. 
- recv_data (torch.Tensor) – The Tensor where data received from pair_rank will be stored. It MUST already be allocated with the correct size if pair_rank is not None. If pair_rank is None, this can be an empty tensor. 
- pair_rank (int or None) – The rank of the process to exchange data with, or None. 
 
 
deepquantum.cutting module#
Circuit cutting
- deepquantum.cutting.transform_cut2move(operators: Sequential, cut_lst: List[Tuple[int, int]], observables: ModuleList | None = None, qpd_form: bool = False) Tuple[Sequential, ModuleList | None]#
- Transform - WireCutto- Moveand expand the observables accordingly.
- deepquantum.cutting.partition_labels(operators: ~torch.nn.modules.container.Sequential, ignore: ~typing.Callable = <function <lambda>>, keep_idle_wires: bool = False) List[int | None]#
- Generate partition labels from the connectivity of a quantum circuit. 
- deepquantum.cutting.map_qubit(qubit_labels: Sequence[Hashable]) Tuple[List[Tuple], Dict[Hashable, List]]#
- Generate a qubit map given a qubit partitioning. 
- deepquantum.cutting.label_operators(operators: Sequential, qubit_map: Sequence[Tuple]) Dict[Hashable, List]#
- Generate a list of operators for each partition of the circuit. 
- deepquantum.cutting.split_barriers(operators: Sequential) Sequential#
- Mutate operators to split barriers into single-qubit barriers. 
- deepquantum.cutting.combine_barriers(operators: Sequential) Sequential#
- Mutate operators to combine barriers with common names into a single barrier. 
- deepquantum.cutting.get_qpd_operators(operators: Sequential, qubit_labels: Sequence[Hashable]) Sequential#
- Replace all nonlocal gates belonging to more than one partition with two-qubit QPD gates. 
- deepquantum.cutting.separate_operators(operators: Sequential, qubit_labels: Sequence[Hashable] | None = None) Dict#
- Separate the circuit into its disconnected components. 
- deepquantum.cutting.decompose_observables(observables: ModuleList, qubit_labels: Sequence[Hashable]) Dict#
- Decompose the observables with respect to qubit partition labels. 
- deepquantum.cutting.partition_problem(operators: Sequential, qubit_labels: Sequence[Hashable] | None = None, observables: ModuleList | None = None) Tuple[Dict, Dict | None]#
- Separate the circuit and observables. 
deepquantum.distributed module#
Distributed operations
- deepquantum.distributed.local_gate(state: Tensor, targets: List[int], matrix: Tensor) Tensor#
- Apply a gate to a state vector locally. 
- deepquantum.distributed.local_many_ctrl_one_targ_gate(state: Tensor, controls: List[int], target: int, matrix: Tensor, derivative: bool = False) Tensor#
- Apply a multi-control single-qubit gate to a state vector locally. - See https://arxiv.org/abs/2311.01512 Alg.3 
- deepquantum.distributed.local_swap_gate(state: Tensor, target1: int, target2: int) Tensor#
- Apply a SWAP gate to a state vector locally. 
- deepquantum.distributed.dist_one_targ_gate(state: DistributedQubitState, target: int, matrix: Tensor) DistributedQubitState#
- Apply a single-qubit gate to a distributed state vector. - See https://arxiv.org/abs/2311.01512 Alg.6 
- deepquantum.distributed.dist_many_ctrl_one_targ_gate(state: DistributedQubitState, controls: List[int], target: int, matrix: Tensor, derivative: bool = False) DistributedQubitState#
- Apply a multi-control single-qubit gate or its derivative to a distributed state vector. - See https://arxiv.org/abs/2311.01512 Alg.7 
- deepquantum.distributed.dist_ctrl_sub(state: DistributedQubitState, controls: List[int], target: int, matrix: Tensor, derivative: bool = False) DistributedQubitState#
- “A subroutine of dist_many_ctrl_one_targ_gate. - See https://arxiv.org/abs/2311.01512 Alg.8 
- deepquantum.distributed.dist_swap_gate(state: DistributedQubitState, qb1: int, qb2: int)#
- Apply a SWAP gate to a distributed state vector. - See https://arxiv.org/abs/2311.01512 Alg.9 
- deepquantum.distributed.get_local_targets(targets: List[int], nqubit_local: int) List[int]#
- deepquantum.distributed.dist_many_targ_gate(state: DistributedQubitState, targets: List[int], matrix: Tensor) DistributedQubitState#
- Apply a multi-qubit gate to a distributed state vector. - See https://arxiv.org/abs/2311.01512 Alg.10 
- deepquantum.distributed.measure_dist(state: DistributedQubitState, shots: int = 1024, with_prob: bool = False, wires: int | List[int] | None = None, block_size: int = 16777216) Dict#
- Measure a distributed state vector. 
- deepquantum.distributed.inner_product_dist(bra: DistributedQubitState, ket: DistributedQubitState) Tensor#
- Get the inner product of two distributed state vectors. 
deepquantum.gate module#
Quantum gates
- class deepquantum.gate.SingleGate(name: str | None = None, nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - Gate- A base class for single-qubit gates. - 参数:
- name (str or None, optional) – The name of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - get_unitary() Tensor#
- Get the global unitary matrix. 
 - op_dist_state(x: DistributedQubitState) DistributedQubitState#
- Perform a forward pass of a gate for a distributed state vector. 
 
- class deepquantum.gate.DoubleGate(name: str | None = None, nqubit: int = 2, wires: List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - Gate- A base class for two-qubit gates. - 参数:
- name (str or None, optional) – The name of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - get_unitary() Tensor#
- Get the global unitary matrix. 
 
- class deepquantum.gate.DoubleControlGate(name: str | None = None, nqubit: int = 2, wires: List[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - DoubleGate- A base class for two-qubit controlled gates. - 参数:
- name (str or None, optional) – The name of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - get_unitary() Tensor#
- Get the global unitary matrix. 
 
- class deepquantum.gate.TripleGate(name: str | None = None, nqubit: int = 3, wires: List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - Gate- A base class for three-qubit gates. - 参数:
- name (str or None, optional) – The name of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 3 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
- class deepquantum.gate.ArbitraryGate(name: str | None = None, nqubit: int = 1, wires: int | List[int] | None = None, minmax: List[int] | None = None, controls: int | List[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - Gate- A base class for customized gates. - 参数:
- name (str or None, optional) – The name of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Only valid when - wiresis- None. Default:- None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - get_unitary() Tensor#
- Get the global unitary matrix. 
 - inverse() ArbitraryGate#
- Get the inversed gate. 
 
- class deepquantum.gate.ParametricSingleGate(name: str | None = None, inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 基类: - SingleGate- A base class for single-qubit gates with parameters. - 参数:
- name (str or None, optional) – The name of the gate. Default: - None
- inputs (Any, optional) – The parameters of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameters are - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - inputs_to_tensor(inputs: Any | None = None) Tensor#
- Convert inputs to torch.Tensor. 
 - update_matrix() Tensor#
- Update the local unitary matrix. 
 - get_derivative(theta: Any) Tensor#
- Get the derivative of the local unitary matrix. 
 - init_para(inputs: Any | None = None) None#
- Initialize the parameters. 
 - inverse() ParametricSingleGate#
- Get the inversed gate. 
 
- class deepquantum.gate.ParametricDoubleGate(name: str | None = None, inputs: Any | None = None, nqubit: int = 2, wires: List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 基类: - DoubleGate- A base class for two-qubit gates with parameters. - 参数:
- name (str or None, optional) – The name of the gate. Default: - None
- inputs (Any, optional) – The parameters of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameters are - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - inputs_to_tensor(inputs: Any | None = None) Tensor#
- Convert inputs to torch.Tensor. 
 - update_matrix() Tensor#
- Update the local unitary matrix. 
 - get_derivative(theta: Any) Tensor#
- Get the derivative of the local unitary matrix. 
 - init_para(inputs: Any | None = None) None#
- Initialize the parameters. 
 - inverse() ParametricDoubleGate#
- Get the inversed gate. 
 
- class deepquantum.gate.U3Gate(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
U3 gate, a generic single-qubit rotation gate with 3 angles. Matrix Representation: \[ \begin{align}\begin{aligned}\newcommand{\th}{\frac{\theta}{2}}\\\begin{split}U_3(\theta, \phi, \lambda) = \begin{pmatrix} \cos\left(\th\right) & -e^{i\lambda}\sin\left(\th\right) \\ e^{i\phi}\sin\left(\th\right) & e^{i(\phi+\lambda)}\cos\left(\th\right) \end{pmatrix}\end{split}\end{aligned}\end{align} \]- 参数:
- inputs (Any, optional) – The parameters of the gate (\(\theta\), \(\phi\) and \(\lambda\)). Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameters are - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - inputs_to_tensor(inputs: Any | None = None) Tuple[Tensor, Tensor, Tensor]#
- Convert inputs to torch.Tensor. 
 - get_matrix(theta: Any, phi: Any, lambd: Any) Tensor#
- Get the local unitary matrix. 
 - update_matrix() Tensor#
- Update the local unitary matrix. 
 - get_derivative(inputs: Any) Tensor#
- Get the derivatives of the local unitary matrix. 
 - init_para(inputs: Any | None = None) None#
- Initialize the parameters. 
 
- class deepquantum.gate.PhaseShift(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Phase shift gate. Matrix Representation: \[\begin{split}P(\theta) = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\theta} \end{pmatrix}\end{split}\]- 参数:
- inputs (Any, optional) – The parameter of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - inputs_to_tensor(inputs: Any | None = None) Tensor#
- Convert inputs to torch.Tensor. 
 - get_matrix(theta: Any) Tensor#
- Get the local unitary matrix. 
 
- class deepquantum.gate.Identity(nqubit: int = 1, wires: int | List[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - Gate- Identity gate. - Matrix Representation: \[\begin{split}I = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - get_unitary() Tensor#
- Get the global unitary matrix. 
 - forward(x: Any) Any#
- Perform a forward pass. 
 
- class deepquantum.gate.PauliX(nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleGate- PauliX gate. - Matrix Representation: \[\begin{split}X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - pattern(nodes: int | List[int], ancilla: List[int]) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.gate.PauliY(nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleGate- PauliY gate. - Matrix Representation: \[\begin{split}Y = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - pattern(nodes: int | List[int], ancilla: List[int]) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.gate.PauliZ(nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleGate- PauliZ gate. - Matrix Representation: \[\begin{split}Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - pattern(nodes: int | List[int], ancilla: List[int]) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.gate.Hadamard(nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleGate- Hadamard gate. - Matrix Representation: \[\begin{split}H = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - pattern(nodes: int | List[int], ancilla: int | List[int]) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.gate.SGate(nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleGate- S gate. - Matrix Representation: \[\begin{split}S = \begin{pmatrix} 1 & 0 \\ 0 & i \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - inverse() SDaggerGate#
- Get the inversed gate. 
 - pattern(nodes: int | List[int], ancilla: List[int]) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.gate.SDaggerGate(nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleGate- S dagger gate. - Matrix Representation: \[\begin{split}S^{\dagger} = \begin{pmatrix} 1 & 0 \\ 0 & -i \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
- class deepquantum.gate.TGate(nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleGate- T gate. - Matrix Representation: \[\begin{split}T = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - inverse() TDaggerGate#
- Get the inversed gate. 
 
- class deepquantum.gate.TDaggerGate(nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleGate- T dagger gate. - Matrix Representation: \[\begin{split}T^{\dagger} = \begin{pmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
- class deepquantum.gate.Rx(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Rx gate, rotation around x-axis. Matrix Representation: \[ \begin{align}\begin{aligned}\newcommand{\th}{\frac{\theta}{2}}\\\begin{split}R_x(\theta) = \begin{pmatrix} \cos\left(\th\right) & -i\sin\left(\th\right) \\ -i\sin\left(\th\right) & \cos\left(\th\right) \end{pmatrix}\end{split}\end{aligned}\end{align} \]- 参数:
- inputs (Any, optional) – The parameter of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local unitary matrix. 
 - pattern(nodes: int | List[int], ancilla: List[int], angle: Any, requires_grad: bool = False) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.gate.Ry(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Ry gate, rotation around y-axis. Matrix Representation: \[ \begin{align}\begin{aligned}\newcommand{\th}{\frac{\theta}{2}}\\\begin{split}R_y(\theta) = \begin{pmatrix} \cos\left(\th\right) & -\sin\left(\th\right) \\ \sin\left(\th\right) & \cos\left(\th\right) \end{pmatrix}\end{split}\end{aligned}\end{align} \]- 参数:
- inputs (Any, optional) – The parameter of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local unitary matrix. 
 - pattern(nodes: int | List[int], ancilla: List[int], angle: Any, requires_grad: bool = False) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.gate.Rz(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Rz gate, rotation around z-axis. Matrix Representation: \[\begin{split}R_z(\theta) = \begin{pmatrix} e^{-i\frac{\theta}{2}} & 0 \\ 0 & e^{i\frac{\theta}{2}} \end{pmatrix}\end{split}\]- 参数:
- inputs (Any, optional) – The parameter of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local unitary matrix. 
 - pattern(nodes: int | List[int], ancilla: List[int], angle: Any, requires_grad: bool = False) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.gate.ProjectionJ(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, plane: str = 'xy', controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Matrix J to project the measurement basis. The matrix \(J(\theta)\) transforms the measurement in the XY, YZ, or ZX plane to Z measurement. The transformation means \(M(\theta)|\psi\rangle = M_z J(\theta)|\psi\rangle\), where \(M_z\) is the Z-basis measurement. Matrix Representation: \[\begin{split}J_{\text{XY}}(\theta) = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & e^{-i\theta} \\ 1 & -e^{-i\theta} \end{pmatrix}\end{split}\]\[ \begin{align}\begin{aligned}\newcommand{\th}{\frac{\theta}{2}}\\\begin{split}J_{\text{YZ}}(\theta) = \frac{1}{\sqrt{2}} \begin{pmatrix} \cos(\th) + \sin(\th) & -i(\cos(\th) - \sin(\th)) \\ \cos(\th) - \sin(\th) & i(\cos(\th) + \sin(\th)) \end{pmatrix}\end{split}\end{aligned}\end{align} \]\[ \begin{align}\begin{aligned}\newcommand{\th}{\frac{\theta}{2}}\\\begin{split}J_{\text{ZX}}(\theta) = \begin{pmatrix} \cos(\th) & \sin(\th) \\ \sin(\th) & -\cos(\th) \end{pmatrix}\end{split}\end{aligned}\end{align} \]- 参数:
- inputs (Any, optional) – The parameter of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- plane (str, optional) – The measurement plane ( - 'xy',- 'yz', or- 'zx'). Default:- 'xy'
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local unitary matrix. 
 
- class deepquantum.gate.CombinedSingleGate(gates: List[SingleGate], name: str | None = None, nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleGate- Combined single-qubit gate. - 参数:
- gates (List[SingleGate]) – The list of single-qubit gates. 
- name (str or None, optional) – The name of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - get_matrix() Tensor#
- Get the local unitary matrix. 
 - update_matrix() Tensor#
- Update the local unitary matrix. 
 - get_derivative(inputs: Any) Tensor#
- Get the derivatives of the local unitary matrix. 
 - update_npara() None#
- Update the number of parameters. 
 - add(gate: SingleGate) None#
- Add a single-qubit gate to the list and update the local unitary matrix. 
 - inverse() CombinedSingleGate#
- Get the inversed gate. 
 
- class deepquantum.gate.CNOT(nqubit: int = 2, wires: List[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 
CNOT gate. Matrix Representation: \[\begin{split}\text{CNOT} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - pattern(nodes: List[int], ancilla: List[int]) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.gate.Swap(nqubit: int = 2, wires: List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - DoubleGate- Swap gate. - Matrix Representation: \[\begin{split}\text{SWAP} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - op_dist_state(x: DistributedQubitState) DistributedQubitState#
- Perform a forward pass of a gate for a distributed state vector. 
 
- class deepquantum.gate.Rxx(inputs: Any | None = None, nqubit: int = 2, wires: List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Rxx gate. Matrix Representation: \[ \begin{align}\begin{aligned}\newcommand{\th}{\frac{\theta}{2}}\\\begin{split}R_{xx}(\theta) = \exp\left(-i \th X{\otimes}X\right) = \begin{pmatrix} \cos\left(\th\right) & 0 & 0 & -i\sin\left(\th\right) \\ 0 & \cos\left(\th\right) & -i\sin\left(\th\right) & 0 \\ 0 & -i\sin\left(\th\right) & \cos\left(\th\right) & 0 \\ -i\sin\left(\th\right) & 0 & 0 & \cos\left(\th\right) \end{pmatrix}\end{split}\end{aligned}\end{align} \]- 参数:
- inputs (Any, optional) – The parameter of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local unitary matrix. 
 
- class deepquantum.gate.Ryy(inputs: Any | None = None, nqubit: int = 2, wires: List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Ryy gate. Matrix Representation: \[ \begin{align}\begin{aligned}\newcommand{\th}{\frac{\theta}{2}}\\\begin{split}R_{yy}(\theta) = \exp\left(-i \th Y{\otimes}Y\right) = \begin{pmatrix} \cos\left(\th\right) & 0 & 0 & i\sin\left(\th\right) \\ 0 & \cos\left(\th\right) & -i\sin\left(\th\right) & 0 \\ 0 & -i\sin\left(\th\right) & \cos\left(\th\right) & 0 \\ i\sin\left(\th\right) & 0 & 0 & \cos\left(\th\right) \end{pmatrix}\end{split}\end{aligned}\end{align} \]- 参数:
- inputs (Any, optional) – The parameter of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local unitary matrix. 
 
- class deepquantum.gate.Rzz(inputs: Any | None = None, nqubit: int = 2, wires: List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Rzz gate. Matrix Representation: \[ \begin{align}\begin{aligned}\newcommand{\th}{\frac{\theta}{2}}\\\begin{split}R_{zz}(\theta) = \exp\left(-i \th Z{\otimes}Z\right) = \begin{pmatrix} e^{-i \th} & 0 & 0 & 0 \\ 0 & e^{i \th} & 0 & 0 \\ 0 & 0 & e^{i \th} & 0 \\ 0 & 0 & 0 & e^{-i \th} \end{pmatrix}\end{split}\end{aligned}\end{align} \]- 参数:
- inputs (Any, optional) – The parameter of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local unitary matrix. 
 
- class deepquantum.gate.Rxy(inputs: Any | None = None, nqubit: int = 2, wires: List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Rxy gate. Matrix Representation: \[ \begin{align}\begin{aligned}\newcommand{\th}{\frac{\theta}{2}}\\\begin{split}R_{xy}(\theta) = \exp\left(-i \th X{\otimes}Y\right) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos\left(\th\right) & -i\sin\left(\th\right) & 0 \\ 0 & -i\sin\left(\th\right) & \cos\left(\th\right) & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}\end{split}\end{aligned}\end{align} \]- 参数:
- inputs (Any, optional) – The parameter of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - get_matrix(theta: Any) Tensor#
- Get the local unitary matrix. 
 
- class deepquantum.gate.ReconfigurableBeamSplitter(inputs: Any | None = None, nqubit: int = 2, wires: List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Reconfigurable Beam Splitter gate. Matrix Representation: \[\begin{split}\text{RBS}(\theta) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos\left(\theta\right) & \sin\left(\theta\right) & 0 \\ 0 & -\sin\left(\theta\right) & \cos\left(\theta\right) & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}\end{split}\]- 参数:
- inputs (Any, optional) – The parameter of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - inputs_to_tensor(inputs: Any | None = None) Tensor#
- Convert inputs to torch.Tensor. 
 - get_matrix(theta: Any) Tensor#
- Get the local unitary matrix. 
 
- class deepquantum.gate.Toffoli(nqubit: int = 3, wires: List[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - TripleGate- Toffoli gate. - Matrix Representation: \[\begin{split}\text{Toffoli} = \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 3 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - get_unitary() Tensor#
- Get the global unitary matrix. 
 - pattern(nodes: List[int], ancilla: List[int]) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.gate.Fredkin(nqubit: int = 3, wires: List[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - TripleGate- Fredkin gate. - Matrix Representation: \[\begin{split}\text{Fredkin} = \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{pmatrix}\end{split}\]- 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 3 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - get_unitary() Tensor#
- Get the global unitary matrix. 
 
- class deepquantum.gate.UAnyGate(unitary: Any, nqubit: int = 1, wires: int | List[int] | None = None, minmax: List[int] | None = None, controls: int | List[int] | None = None, name: str = 'UAnyGate', den_mat: bool = False, tsr_mode: bool = False)#
- 
Arbitrary unitary gate. - 参数:
- unitary (Any) – Any given unitary matrix. 
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Only valid when - wiresis- None. Default:- None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- name (str, optional) – The name of the gate. Default: - 'UAnyGate'
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - update_matrix() Tensor#
- Update the local unitary matrix. 
 
- class deepquantum.gate.LatentGate(inputs: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, minmax: List[int] | None = None, controls: int | List[int] | None = None, name: str = 'LatentGate', den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Latent gate. - 参数:
- inputs (Any, optional) – Any given real matrix. 
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Only valid when - wiresis- None. Default:- None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- name (str, optional) – The name of the gate. Default: - 'LatentGate'
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameters are - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - inputs_to_tensor(inputs: Any | None = None) Tensor#
- Convert inputs to torch.Tensor. 
 - get_matrix(inputs: Any) Tensor#
- Get the local unitary matrix. 
 - update_matrix() Tensor#
- Update the local unitary matrix. 
 - get_derivative(latent: Any) Tensor#
- Get the derivatives of the local unitary matrix. 
 - init_para(inputs: Any | None = None) None#
- Initialize the parameters. 
 
- class deepquantum.gate.HamiltonianGate(hamiltonian: Any, t: Any | None = None, nqubit: int = 1, wires: int | List[int] | None = None, minmax: List[int] | None = None, controls: int | List[int] | None = None, name: str = 'HamiltonianGate', den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = False)#
- 
Hamiltonian gate. - 参数:
- hamiltonian (Any) – The Hamiltonian. It can be a list, e.g., - [[0.5, 'x0y1'], [-1, 'z3y1']]for \(0.5 * \sigma^x_0 \otimes \sigma^y_1 - \sigma^y_1 \otimes \sigma^z_3\). It can also be a torch.Tensor when- wiresor- minmaxis specified.
- t (Any, optional) – The evolution time. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Only valid when - hamiltonianis not a list. Default:- None
- minmax (List[int] or None, optional) – The minimum and maximum indices of the qubits that the quantum operation acts on. Only valid when - hamiltonianis not a list and- wiresis- None. Default:- None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- name (str, optional) – The name of the gate. Default: - 'HamiltonianGate'
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - to(arg: Any) HamiltonianGate#
- Set dtype or device of the - HamiltonianGate.
 - get_minmax(hamiltonian: List) List[int]#
- Get - minmaxaccording to the Hamiltonian.
 - inputs_to_tensor(inputs: List | None = None) Tuple[Tensor, Tensor]#
- Convert inputs to torch.Tensor. 
 - get_matrix(hamiltonian: Any, t: Any) Tensor#
- Get the local unitary matrix. 
 - update_matrix() Tensor#
- Update the local unitary matrix. 
 - get_derivative(t: Any) Tensor#
- Get the derivative of the local unitary matrix. 
 - init_para(inputs: List | None = None) None#
- Initialize the parameters. 
 
- class deepquantum.gate.Reset(nqubit: int = 1, wires: int | List[int] | None = None, postselect: int | None = 0, tsr_mode: bool = False)#
- 基类: - Gate- Reset. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- postselect (int or None, optional) – The postselected value. Default: 0 ( - Nonemeans no postselection, which is not compatible with vmap)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - op_state(x: Tensor) Tensor#
- Perform a forward pass for state vectors. 
 
- class deepquantum.gate.Barrier(nqubit: int = 1, wires: int | List[int] | None = None, name: str = 'Barrier')#
- 基类: - Gate- Barrier. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- name (str, optional) – The name of the gate. Default: - 'Barrier'
 
 - forward(x: Any) Any#
- Perform a forward pass. 
 - pattern(nodes: List[int], ancilla: List[int]) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.gate.WireCut(nqubit: int = 1, wires: int | List[int] = 0)#
- 基类: - Barrier- Wire Cut. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int or List[int], optional) – The indices of the qubits that the quantum operation acts on. Default: 0 
 
 
- class deepquantum.gate.Move(nqubit: int = 2, wires: List[int] | None = None, postselect: int | None = 0, tsr_mode: bool = False)#
- 基类: - DoubleGate- Move, a two-qubit operation representing a reset of the second qubit followed by a swap. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- postselect (int or None, optional) – The postselected value. Default: 0 ( - Nonemeans no postselection, which is not compatible with vmap)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - forward(x: Tensor) Tensor#
- Perform a forward pass. 
 
deepquantum.layer module#
Quantum layers
- class deepquantum.layer.SingleLayer(name: str | None = None, nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - Layer- A base class for layers of single-qubit gates. - 参数:
- name (str, optional) – The name of the layer. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - get_unitary() Tensor#
- Get the global unitary matrix. 
 
- class deepquantum.layer.ParametricSingleLayer(name: str | None = None, nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)#
- 基类: - SingleLayer- A base class for layers of single-qubit gates with parameters. - 参数:
- name (str or None, optional) – The name of the layer. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameters are - nn.Parameteror- buffer. Default:- True(which means- nn.Parameter)
 
 - inverse() ParametricSingleLayer#
- Get the inversed layer. 
 - pattern(nodes: List[List[int]], ancilla: List[List[int]], angle: List[Any], requires_grad: bool = False) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.layer.DoubleLayer(name: str | None = None, nqubit: int = 2, wires: List[List[int]] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - Layer- A base class for layers of two-qubit gates. - 参数:
- name (str, optional) – The name of the layer. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
- class deepquantum.layer.Observable(nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, basis: str = 'z', den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleLayer- A - Layerthat represents an observable which can be expressed by Pauli string.- 参数:
- nqubit (int, optional) – The number of qubits in the circuit. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The wires to measure. Default: - None(which means all wires are measured)
- basis (str, optional) – The measurement basis for each wire. It can be - 'x',- 'y', or- 'z'. If only one character is given, it is repeated for all wires. Default:- 'z'
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
- class deepquantum.layer.U3Layer(nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, inputs: Any | None = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)#
- 
A layer of U3 gates. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- inputs (Any, optional) – The parameters of the layer. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameters are - nn.Parameteror- buffer. Default:- True(which means- nn.Parameter)
 
 
- class deepquantum.layer.XLayer(nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleLayer- A layer of Pauli-X gates. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
- class deepquantum.layer.YLayer(nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleLayer- A layer of Pauli-Y gates. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
- class deepquantum.layer.ZLayer(nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleLayer- A layer of Pauli-Z gates. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
- class deepquantum.layer.HLayer(nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - SingleLayer- A layer of Hadamard gates. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
- class deepquantum.layer.RxLayer(nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, inputs: Any | None = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)#
- 
A layer of Rx gates. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- inputs (Any, optional) – The parameters of the layer. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameters are - nn.Parameteror- buffer. Default:- True(which means- nn.Parameter)
 
 
- class deepquantum.layer.RyLayer(nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, inputs: Any | None = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)#
- 
A layer of Ry gates. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- inputs (Any, optional) – The parameters of the layer. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameters are - nn.Parameteror- buffer. Default:- True(which means- nn.Parameter)
 
 
- class deepquantum.layer.RzLayer(nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, inputs: Any | None = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)#
- 
A layer of Rz gates. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- inputs (Any, optional) – The parameters of the layer. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameters are - nn.Parameteror- buffer. Default:- True(which means- nn.Parameter)
 
 
- class deepquantum.layer.CnotLayer(nqubit: int = 2, wires: List[List[int]] | None = None, name: str = 'CnotLayer', den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - DoubleLayer- A layer of CNOT gates. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- name (str, optional) – The name of the layer. Default: - 'CnotLayer'
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
- class deepquantum.layer.CnotRing(nqubit: int = 2, minmax: List[int] | None = None, step: int = 1, reverse: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - CnotLayer- A layer of CNOT gates in a cyclic way. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- minmax (List[int] or None, optional) – The minimum and maximum indices of qubits that the quantum operation acts on. Default: - None
- step (int, optional) – The distance between the control and target qubits of each CNOT gate. Default: 1 
- reverse (bool, optional) – Whether the CNOT gates are applied from the maximum to the minimum index or vice versa. Default: - False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
deepquantum.operation module#
Base classes
- class deepquantum.operation.Operation(name: str | None = None, nqubit: int = 1, wires: int | List[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - Module- A base class for quantum operations. - 参数:
- name (str or None, optional) – The name of the quantum operation. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - tensor_rep(x: Tensor) Tensor#
- Get the tensor representation of the state. 
 - vector_rep(x: Tensor) Tensor#
- Get the vector representation of the state. 
 - matrix_rep(x: Tensor) Tensor#
- Get the density matrix representation of the state. 
 - get_unitary() Tensor#
- Get the global unitary matrix. 
 - init_para() None#
- Initialize the parameters. 
 - set_nqubit(nqubit: int) None#
- Set the number of qubits of the - Operation.
 - set_wires(wires: int | List[int]) None#
- Set the wires of the - Operation.
 - forward(x: Tensor) Tensor#
- Perform a forward pass. 
 
- class deepquantum.operation.Gate(name: str | None = None, nqubit: int = 1, wires: int | List[int] | None = None, controls: int | List[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - Operation- A base class for quantum gates. - 参数:
- name (str or None, optional) – The name of the gate. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- controls (int, List[int] or None, optional) – The indices of the control qubits. Default: - None
- condition (bool, optional) – Whether to use - controlsas conditional measurement. Default:- False
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - set_controls(controls: int | List[int]) None#
- Set the control wires of the - Operation.
 - get_matrix(inputs: Any) Tensor#
- Get the local unitary matrix. 
 - update_matrix() Tensor#
- Update the local unitary matrix. 
 - get_derivative(inputs: Any) Tensor#
- Get the derivative of the local unitary matrix. 
 - op_state(x: Tensor) Tensor#
- Perform a forward pass for state vectors. 
 - op_state_base(x: Tensor, matrix: Tensor) Tensor#
- Perform a forward pass of a gate for state vectors. 
 - op_state_control(x: Tensor, matrix: Tensor) Tensor#
- Perform a forward pass of a controlled gate for state vectors. 
 - op_den_mat(x: Tensor) Tensor#
- Perform a forward pass for density matrices. 
 - op_den_mat_base(x: Tensor, matrix: Tensor) Tensor#
- Perform a forward pass of a gate for density matrices. 
 - op_den_mat_control(x: Tensor, matrix: Tensor) Tensor#
- Perform a forward pass of a controlled gate for density matrices. 
 - op_dist_state(x: DistributedQubitState) DistributedQubitState#
- Perform a forward pass of a gate for a distributed state vector. 
 - forward(x: Tensor | MatrixProductState | DistributedQubitState) Tensor | MatrixProductState | DistributedQubitState#
- Perform a forward pass. 
 - get_mpo() Tuple[List[Tensor], int]#
- Convert gate to MPO form with identities at empty sites. - 备注 - If sites are not adjacent, insert identities in the middle, i.e., - >>> | | | | | >>> --A---x---B-- -> --A---I---B-- >>> | | | | | - where - >>> a >>> | >>> --i--I--j-- >>> | >>> b - means \(\delta_{i,j} \delta_{a,b}\) 
 - op_mps(mps: MatrixProductState) MatrixProductState#
- Perform a forward pass for the - MatrixProductState.
 
- class deepquantum.operation.Layer(name: str | None = None, nqubit: int = 1, wires: int | List[int] | List[List[int]] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - Operation- A base class for quantum layers. - 参数:
- name (str, optional) – The name of the layer. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int], List[List[int]] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - get_unitary() Tensor#
- Get the global unitary matrix. 
 - init_para(inputs: Any | None = None) None#
- Initialize the parameters. 
 - update_npara() None#
- Update the number of parameters. 
 - set_nqubit(nqubit: int) None#
- Set the number of qubits of the - Layer.
 - set_wires(wires: int | List[int] | List[List[int]]) None#
- Set the wires of the - Layer.
 - forward(x: Tensor | MatrixProductState | DistributedQubitState) Tensor | MatrixProductState | DistributedQubitState#
- Perform a forward pass. 
 - pattern(nodes: List[List[int]], ancilla: List[List[int]]) Sequential#
- Get the MBQC pattern. 
 
- class deepquantum.operation.Channel(inputs: Any | None = None, name: str | None = None, nqubit: int = 1, wires: int | List[int] | None = None, tsr_mode: bool = False, requires_grad: bool = False)#
- 基类: - Operation- A base class for quantum channels. - 参数:
- inputs (Any, optional) – The parameter of the channel. Default: - None
- name (str or None, optional) – The name of the channel. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
- requires_grad (bool, optional) – Whether the parameter is - nn.Parameteror- buffer. Default:- False(which means- buffer)
 
 - property prob#
- The error probability. 
 - inputs_to_tensor(inputs: Any | None = None) Tensor#
- Convert inputs to torch.Tensor. 
 - get_matrix(theta: Any) Tensor#
- Update the local Kraus matrices acting on density matrices. 
 - update_matrix() Tensor#
- Update the local Kraus matrices acting on density matrices. 
 - init_para(inputs: Any | None = None) None#
- Initialize the parameters. 
 - op_den_mat(x: Tensor) Tensor#
- Perform a forward pass for density matrices. 
 - forward(x: Tensor) Tensor#
- Perform a forward pass. 
 
- class deepquantum.operation.GateQPD(bases: List[Tuple[Sequential, ...]], coeffs: List[float], label: int | None = None, name: str | None = None, nqubit: int = 1, wires: int | List[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - Gate- A base class for quasiprobability-decomposition gates. - 参数:
- bases (List[Tuple[nn.Sequential, ...]]) – A list of tuples describing the operations probabilistically used to simulate an ideal quantum operation. 
- coeffs (List[float]) – The coefficients for quasiprobability representation. 
- label (int or None, optional) – The label of the gate. Default: - None
- name (str or None, optional) – The name of the quantum operation. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - set_nqubit(nqubit: int) None#
- Set the number of qubits of the - GateQPD.
 - set_wires(wires: int | List[int]) None#
- Set the wires of the - GateQPD.
 - forward(x: Tensor, idx: int | None = None) Tensor#
- Perform a forward pass. - 参数:
- x (torch.Tensor) – The input tensor. 
- idx (int, optional) – The index of the operation to be applied. Default: 0 
 
 
 
- class deepquantum.operation.MeasureQPD(nqubit: int = 1, wires: int | List[int] | None = None)#
- 基类: - Operation- A operation for denoting a QPD measurement location. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (int, List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
 
 - forward(x: Any) Any#
- Perform a forward pass. 
 
deepquantum.optimizer module#
Optimizer: various on-chip optimization algorthims
- class deepquantum.optimizer.Optimizer(target_func, param_init, random_state=0)#
- 基类: - object- A base class for optimizers. - 参数:
- target_func (function) – The target function to optimize, more specifically, to minimize. It is supposed to accept - kwargsin the format of- param_initas inputs.
- param_init (Dict, List, np.ndarray or torch.Tensor) – The initial parameters for the target function. The keys of it should be consistent with inputs of - target_func.
- random_state (int) – The random seed for this optimization process. 
 
 
- class deepquantum.optimizer.OptimizerBayesian(target_func, param_init, random_state=0)#
- 基类: - Optimizer- Optimizer based on Bayesian optimization. - See bayesian-optimization/BayesianOptimization. - 参数:
- target_func (function) – The target function to optimize, more specifically, to minimize. It is supposed to accept - kwargsin the format of- param_initas inputs.
- param_init (Dict, List, np.ndarray or torch.Tensor) – The initial parameters for the target function. The keys of it should be consistent with inputs of - target_func.
- random_state (int) – The random seed for this optimization process. 
 
 - 备注 - In the scenerio of on-chip optimization, the periods of phase shifters are all from 0 to \(2\pi\), so in this program the - pbound(a parameter determining the search region in Bayesian-Optimization package) is fixed from 0 to \(2\pi\).- gen_pbounds() Dict#
 - param_suggest() ndarray#
 - param_register(param_array: ndarray, target: float) None#
 - run(nstep: int, if_print: bool = False) List#
 
- class deepquantum.optimizer.OptimizerSPSA(target_func, param_init, random_state=0)#
- 基类: - Optimizer- Optimizer based on SPSA (Simultaneous Perturbation Stochastic Approximation). - See https://www.jhuapl.edu/spsa/Pages/MATLAB.htm. - 参数:
- target_func (function) – The target function to optimize, more specifically, to minimize. It is supposed to accept - kwargsin the format of- param_initas inputs.
- param_init (Dict, List, np.ndarray or torch.Tensor) – The initial parameters for the target function. The keys of it should be consistent with inputs of - target_func.
- random_state (int) – The random seed for this optimization process. 
 
 - set_hyperparam(hyperparam: Dict) None#
- Set hyperparameters whose keys include - 'a',- 'c',- 'A',- 'nepoch',- 'alpha',- 'gamma'.
 - param_suggest() ndarray#
 - param_register(param_array: ndarray | List, target: ndarray | List) None#
 - ori_random_state() None#
 - run(nstep: int, if_print: bool = False) List#
 
- class deepquantum.optimizer.OptimizerFourier(target_func, param_init, order=5, lr=0.1, random_state=0)#
- 基类: - Optimizer- Optimizer based on Fourier series. - Obtain the gradient approximation from the target function approximation. - 参数:
- target_func (function) – The target function to optimize, more specifically, to minimize. It is supposed to accept - kwargsin the format of- param_initas inputs.
- param_init (Dict, List, np.ndarray or torch.Tensor) – The initial parameters for the target function. The keys of it should be consistent with inputs of - target_func.
- order (int) – The order of Fourier series to approximate. 
- lr (float) – The step length (or equivalently, learning rate) of the learning process (namely, gradient descent process). 
- random_state (int) – The random seed for this optimization process. 
 
 - gen_a() ndarray#
 - param_suggest() ndarray#
 - param_register(param_array: ndarray, target: ndarray)#
 - run(nstep: int, if_print: bool = False) List#
 
deepquantum.qasm3 module#
- deepquantum.qasm3.cir_to_qasm3(circuit: QubitCircuit) str#
- Converts - QubitCircuitto an OpenQASM 3.0 string.- 参数:
- circuit (QubitCircuit) – The DeepQuantum circuit to convert. 
- 返回:
- A string containing the OpenQASM 3.0 representation of the circuit. 
- 返回类型:
- str 
 
- class deepquantum.qasm3.GateDefinition(name: str, params: List[str], qubits: List[str], body: List[str])#
- 基类: - object
- deepquantum.qasm3.qasm3_to_cir(qasm_string: str) QubitCircuit#
- Converts a full-featured OpenQASM 3.0 string to - QubitCircuit. Supports: def, inv @, ctrl @, and floating-point/negative pow() @.
deepquantum.qmath module#
Common functions
- deepquantum.qmath.is_power_of_two(n: int) bool#
- Check if an integer is a power of two. 
- deepquantum.qmath.is_power(n: int, base: int) bool#
- Check if an integer is a power of the given base. 
- deepquantum.qmath.int_to_bitstring(x: int, n: int, debug: bool = False) str#
- Convert from integer to bit string. 
- deepquantum.qmath.list_to_decimal(digits: List[int], base: int) int#
- Convert from list of digits to decimal integer. 
- deepquantum.qmath.decimal_to_list(n: int, base: int, ndigit: int | None = None) List[int]#
- Convert from decimal integer to list of digits. 
- deepquantum.qmath.inverse_permutation(permute_shape: List[int]) List[int]#
- Calculate the inversed permutation. - 参数:
- permute_shape (List[int]) – Shape of permutation. 
- 返回:
- A list of integers that is the inverse of - permute_shape.
- 返回类型:
- List[int] 
 
- deepquantum.qmath.is_unitary(matrix: Tensor, rtol: float = 1e-05, atol: float = 0.0001) bool#
- Check if a tensor is a unitary matrix. - 参数:
- matrix (torch.Tensor) – Square matrix. 
- 返回:
- Trueif- matrixis unitary,- Falseotherwise.
- 返回类型:
- bool 
 
- deepquantum.qmath.is_density_matrix(rho: Tensor) bool#
- Check if a tensor is a valid density matrix. - A density matrix is a positive semi-definite Hermitian matrix with trace one. - 参数:
- rho (torch.Tensor) – The tensor to check. It can be either 2D or 3D. If 3D, the first dimension is assumed to be the batch dimension. 
- 返回:
- Trueif the tensor is a density matrix,- Falseotherwise.
- 返回类型:
- bool 
 
- deepquantum.qmath.is_positive_definite(mat: Tensor) bool#
- Check if the matrix is positive definite 
- deepquantum.qmath.safe_inverse(x: Any, epsilon: float = 1e-12) Any#
- Safe inversion. 
- class deepquantum.qmath.SVD(*args, **kwargs)#
- 基类: - Function- Customized backward of SVD for better numerical stability. - Modified from wangleiphy/tensorgrad See https://readpaper.com/paper/2971614414 - generate_vmap_rule = True#
 - static forward(a)#
- Define the forward of the custom autograd Function. - This function is to be overridden by all subclasses. There are two ways to define forward: - Usage 1 (Combined forward and ctx): - @staticmethod def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any: pass - It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types). 
- See combining-forward-context for more details 
 - Usage 2 (Separate forward and ctx): - @staticmethod def forward(*args: Any, **kwargs: Any) -> Any: pass @staticmethod def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None: pass - The forward no longer accepts a ctx argument. 
- Instead, you must also override the - torch.autograd.Function.setup_context()staticmethod to handle setting up the- ctxobject.- outputis the output of the forward,- inputsare a Tuple of inputs to the forward.
- See extending-autograd for more details 
 - The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with - ctx.save_for_backward()if they are intended to be used in- backward(equivalently,- vjp) or- ctx.save_for_forward()if they are intended to be used for in- jvp.
 - static setup_context(ctx, inputs, output)#
- There are two ways to define the forward pass of an autograd.Function. - Either: - Override forward with the signature - forward(ctx, *args, **kwargs).- setup_contextis not overridden. Setting up the ctx for backward happens inside the- forward.
- Override forward with the signature - forward(*args, **kwargs)and override- setup_context. Setting up the ctx for backward happens inside- setup_context(as opposed to inside the- forward)
 - See - torch.autograd.Function.forward()and extending-autograd for more details.
 - static backward(ctx, du, ds, dvh)#
- Define a formula for differentiating the operation with backward mode automatic differentiation. - This function is to be overridden by all subclasses. (Defining this function is equivalent to defining the - vjpfunction.)- It must accept a context - ctxas the first argument, followed by as many outputs as the- forward()returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs to- forward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.- The context can be used to retrieve tensors saved during the forward pass. It also has an attribute - ctx.needs_input_gradas a tuple of booleans representing whether each input needs gradient. E.g.,- backward()will have- ctx.needs_input_grad[0] = Trueif the first input to- forward()needs gradient computed w.r.t. the output.
 
- deepquantum.qmath.torchqr_grad(a, q, r, dq, dr)#
- Get the gradient for QR. 
- class deepquantum.qmath.QR(*args, **kwargs)#
- 基类: - Function- Customized backward of QR for better numerical stability. - generate_vmap_rule = True#
 - static forward(a)#
- Define the forward of the custom autograd Function. - This function is to be overridden by all subclasses. There are two ways to define forward: - Usage 1 (Combined forward and ctx): - @staticmethod def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any: pass - It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types). 
- See combining-forward-context for more details 
 - Usage 2 (Separate forward and ctx): - @staticmethod def forward(*args: Any, **kwargs: Any) -> Any: pass @staticmethod def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None: pass - The forward no longer accepts a ctx argument. 
- Instead, you must also override the - torch.autograd.Function.setup_context()staticmethod to handle setting up the- ctxobject.- outputis the output of the forward,- inputsare a Tuple of inputs to the forward.
- See extending-autograd for more details 
 - The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with - ctx.save_for_backward()if they are intended to be used in- backward(equivalently,- vjp) or- ctx.save_for_forward()if they are intended to be used for in- jvp.
 - static setup_context(ctx, inputs, output)#
- There are two ways to define the forward pass of an autograd.Function. - Either: - Override forward with the signature - forward(ctx, *args, **kwargs).- setup_contextis not overridden. Setting up the ctx for backward happens inside the- forward.
- Override forward with the signature - forward(*args, **kwargs)and override- setup_context. Setting up the ctx for backward happens inside- setup_context(as opposed to inside the- forward)
 - See - torch.autograd.Function.forward()and extending-autograd for more details.
 - static backward(ctx, dq, dr)#
- Define a formula for differentiating the operation with backward mode automatic differentiation. - This function is to be overridden by all subclasses. (Defining this function is equivalent to defining the - vjpfunction.)- It must accept a context - ctxas the first argument, followed by as many outputs as the- forward()returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs to- forward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.- The context can be used to retrieve tensors saved during the forward pass. It also has an attribute - ctx.needs_input_gradas a tuple of booleans representing whether each input needs gradient. E.g.,- backward()will have- ctx.needs_input_grad[0] = Trueif the first input to- forward()needs gradient computed w.r.t. the output.
 
- deepquantum.qmath.split_tensor(tensor: Tensor, center_left: bool = True) Tuple[Tensor, Tensor]#
- Split a tensor by QR. 
- deepquantum.qmath.state_to_tensors(state: Tensor, nsite: int, qudit: int = 2) List[Tensor]#
- Convert a quantum state to a list of tensors. 
- deepquantum.qmath.slice_state_vector(state: Tensor, nqubit: int, wires: List[int], bits: str, normalize: bool = True) Tensor#
- Get the sliced state vectors according to - wiresand- bits.
- deepquantum.qmath.multi_kron(lst: List[Tensor]) Tensor#
- Calculate the Kronecker/tensor/outer product for a list of tensors. - 参数:
- lst (List[torch.Tensor]) – A list of tensors. 
- 返回:
- The Kronecker/tensor/outer product of the input. 
- 返回类型:
- torch.Tensor 
 
- deepquantum.qmath.partial_trace(rho: Tensor, nqudit: int, trace_lst: List[int], qudit: int = 2) Tensor#
- Calculate the partial trace for a batch of density matrices. - 参数:
- rho (torch.Tensor) – Density matrices with the shape of \((\text{batch}, \text{qudit}^{\text{nqudit}}, \text{qudit}^{\text{nqudit}})\). 
- nqudit (int) – Total number of qudits. 
- trace_lst (List[int]) – A list of qudits to be traced. 
- qudit (int, optional) – The dimension of the qudits. Default: 2 
 
- 返回:
- Reduced density matrices. 
- 返回类型:
- torch.Tensor 
 
- deepquantum.qmath.amplitude_encoding(data: Any, nqubit: int) Tensor#
- Encode data into quantum states using amplitude encoding. - This function takes a batch of data and encodes each sample into a quantum state using amplitude encoding. The quantum state is represented by a complex-valued tensor of shape \((\text{batch}, 2^{\text{nqubit}})\). The data is normalized to have unit norm along the last dimension before encoding. If the data size is smaller than \(2^{\text{nqubit}}\), the remaining amplitudes are set to zero. If the data size is larger than \(2^{\text{nqubit}}\), only the first \(2^{\text{nqubit}}\) elements are used. - 参数:
- data (torch.Tensor or array-like) – The input data to be encoded. It should have shape \((\text{batch}, ...)\) where \(...\) can be any dimensions. If it is not a torch.Tensor object, it will be converted to one. 
- nqubit (int) – The number of qubits to use for encoding. 
 
- 返回:
- The encoded quantum states as complex-valued tensors of shape \((\text{batch}, 2^{\text{nqubit}}, 1)\). 
- 返回类型:
- torch.Tensor 
 - 示例 - >>> data = [[0.5, 0.5], [0.7, 0.3]] >>> amplitude_encoding(data, nqubit=2) tensor([[[0.7071+0.j], [0.7071+0.j], [0.0000+0.j], [0.0000+0.j]], [[0.9487+0.j], [0.3162+0.j], [0.0000+0.j], [0.0000+0.j]]]) 
- deepquantum.qmath.evolve_state(state: Tensor, matrix: Tensor, nqudit: int, wires: List[int], qudit: int = 2) Tensor#
- Perform the evolution of quantum states. - 参数:
- state (torch.Tensor) – The batched state tensor. 
- matrix (torch.Tensor) – The evolution matrix. 
- nqudit (int) – The number of the qudits. 
- wires (List[int]) – The indices of the qudits that the quantum operation acts on. 
- qudit (int, optional) – The dimension of the qudits. Default: 2 
 
 
- deepquantum.qmath.evolve_den_mat(state: Tensor, matrix: Tensor, nqudit: int, wires: List[int], qudit: int = 2) Tensor#
- Perform the evolution of density matrices. - 参数:
- state (torch.Tensor) – The batched state tensor. 
- matrix (torch.Tensor) – The evolution matrix. 
- nqudit (int) – The number of the qudits. 
- wires (List[int]) – The indices of the qudits that the quantum operation acts on. 
- qudit (int, optional) – The dimension of the qudits. Default: 2 
 
 
- deepquantum.qmath.block_sample(probs: Tensor, shots: int = 1024, block_size: int = 16777216) List#
- Sample from a probability distribution using block sampling. - 参数:
- probs (torch.Tensor) – The probability distribution to sample from. 
- shots (int, optional) – The number of samples to draw. Default: 1024 
- block_size (int, optional) – The block size for sampling. Default: 2 ** 24 
 
 
- deepquantum.qmath.measure(state: Tensor, shots: int = 1024, with_prob: bool = False, wires: int | List[int] | None = None, den_mat: bool = False, block_size: int = 16777216) Dict | List[Dict]#
- A function that performs a measurement on a quantum state and returns the results. - The measurement is done by sampling from the probability distribution of the quantum state. The results are given as a dictionary or a list of dictionaries, where each key is a bit string representing the measurement outcome, and each value is either the number of occurrences or a tuple of the number of occurrences and the probability. - 参数:
- state (torch.Tensor) – The quantum state to measure. It can be a tensor of shape \((2^n,)\) or \((2^n, 1)\) representing a state vector, or a tensor of shape \((\text{batch}, 2^n)\) or \((\text{batch}, 2^n, 1)\) representing a batch of state vectors. It can also be a tensor of shape \((2^n, 2^n)\) representing a density matrix or \((\text{batch}, 2^n, 2^n)\) representing a batch of density matrices. 
- shots (int, optional) – The number of times to sample from the quantum state. Default: 1024 
- with_prob (bool, optional) – A flag that indicates whether to return the probabilities along with the number of occurrences. Default: - False
- wires (int, List[int] or None, optional) – The wires to measure. It can be an integer or a list of integers specifying the indices of the wires. Default: - None(which means all wires are measured)
- den_mat (bool, optional) – Whether the state is a density matrix or not. Default: - False
- block_size (int, optional) – The block size for sampling. Default: 2 ** 24 
 
- 返回:
- The measurement results. If the state is a single state vector, it returns a dictionary where each key is a bit string representing the measurement outcome, and each value is either the number of occurrences or a tuple of the number of occurrences and the probability. If the state is a batch of state vectors, it returns a list of dictionaries with the same format for each state vector in the batch. 
- 返回类型:
- Union[Dict, List[Dict]] 
 
- deepquantum.qmath.sample_sc_mcmc(prob_func: Callable, proposal_sampler: Callable, shots: int = 1024, num_chain: int = 5) defaultdict#
- Get the samples of the probability distribution function via SC-MCMC method. 
- deepquantum.qmath.get_prob_mps(mps_lst: List[Tensor], wire: int) Tensor#
- Calculate the probability distribution (|0⟩ and |1⟩ probabilities) for a specific wire in an MPS. - This function computes the probability of measuring |0⟩ and |1⟩ for the k-th qubit in a quantum state represented as a Matrix Product State (MPS). It does this by: 1. Contracting the tensors to the left of the target tensor 2. Contracting the tensors to the right of the target tensor 3. Computing the final contraction with the target tensor - 参数:
- wire (int) – Index of the target qubit to compute probabilities for 
- mps_lst (List[torch.Tensor]) – List of MPS tensors representing the quantum state Each 3-dimensional tensor should have shape (bond_dim_left, physical_dim, bond_dim_right) 
 
- 返回:
- A tensor containing [P(|0⟩), P(|1⟩)] probabilities for the target qubit 
- 返回类型:
- torch.Tensor 
 
- deepquantum.qmath.inner_product_mps(tensors0: List[Tensor], tensors1: List[Tensor], form: str = 'norm') Tensor | List[Tensor]#
- Computes the inner product of two matrix product states. - 参数:
- tensors0 (List[torch.Tensor]) – The tensors of the first MPS, each with shape \((..., d_0, d_1, d_2)\), where \(d_0\) is the bond dimension of the left site, \(d_1\) is the physical dimension, and \(d_2\) is the bond dimension of the right site. 
- tensors1 (List[torch.Tensor]) – The tensors of the second MPS, each with shape \((..., d_0, d_1, d_2)\), where \(d_0\) is the bond dimension of the left site, \(d_1\) is the physical dimension, and \(d_2\) is the bond dimension of the right site. 
- form (str, optional) – The form of the output. If - 'log', returns the logarithm of the absolute value of the inner product. If- 'list', returns a list of norms at each step. Otherwise, returns the inner product as a scalar. Default:- 'norm'
 
- 返回:
- The inner product of the two MPS, or a list of norms at each step. 
- 返回类型:
- Union[torch.Tensor, List[torch.Tensor]] 
- 抛出:
- AssertionError – If the tensors have incompatible shapes or lengths. 
 
- deepquantum.qmath.expectation(state: Tensor | List[Tensor], observable: Observable, den_mat: bool = False, chi: int | None = None) Tensor#
- A function that calculates the expectation value of an observable on a quantum state. - The expectation value is the average measurement outcome of the observable on the quantum state. It is a real number that represents the mean of the probability distribution of the measurement outcomes. - 参数:
- state (torch.Tensor or List[torch.Tensor]) – The quantum state to measure. It can be a list of tensors representing a matrix product state, or a tensor representing a density matrix or a state vector. 
- observable (Observable) – The observable to measure. It is an instance of - Observableclass that implements the measurement basis and the corresponding gates.
- den_mat (bool, optional) – Whether to use density matrix representation. Default: - False
- chi (int or None, optional) – The bond dimension of the matrix product state. It is only used when the state is a list of tensors. Default: - None(which means no truncation)
 
- 返回:
- The expectation value of the observable on the quantum state. It is a scalar tensor with real values. 
- 返回类型:
- torch.Tensor 
 
- deepquantum.qmath.sample2expval(sample: Dict) Tensor#
- Get the expectation value according to the measurement results. 
- deepquantum.qmath.meyer_wallach_measure(state_tsr: Tensor) Tensor#
- Calculate Meyer-Wallach entanglement measure. - See https://readpaper.com/paper/2945680873 Eq.(19) - 参数:
- state_tsr (torch.Tensor) – Input with the shape of \((\text{batch}, 2, ..., 2)\). 
- 返回:
- The value of Meyer-Wallach measure. 
- 返回类型:
- torch.Tensor 
 
- deepquantum.qmath.linear_map_mw(state_tsr: Tensor, j: int, b: int) Tensor#
- Calculate the linear mapping for Meyer-Wallach measure. - See https://readpaper.com/paper/2945680873 Eq.(18) - 备注 - Project on state with local projectors on the - jth qubit. See https://arxiv.org/pdf/quant-ph/0305094.pdf Eq.(2)- 参数:
- state_tsr (torch.Tensor) – Input with the shape of \((\text{batch}, 2, ..., 2)\). 
- j (int) – The - jth qubit to project on, from \(0\) to \(\text{nqubit}-1\).
- b (int) – The basis of projection, \(\ket{0}\) or \(\ket{1}\). 
 
- 返回:
- Non-normalized state tensor after the linear mapping. 
- 返回类型:
- torch.Tensor 
 
- deepquantum.qmath.generalized_distance(state1: Tensor, state2: Tensor) Tensor#
- Calculate the generalized distance. - See https://readpaper.com/paper/2945680873 Eq.(20) - 备注 - Implemented according to https://arxiv.org/pdf/quant-ph/0310137.pdf Eq.(4) - 参数:
- state1 (torch.Tensor) – Input with the shape of \((\text{batch}, 2^n, 1)\). 
- state2 (torch.Tensor) – Input with the shape of \((\text{batch}, 2^n, 1)\). 
 
- 返回:
- The generalized distance. 
- 返回类型:
- torch.Tensor 
 
- deepquantum.qmath.meyer_wallach_measure_brennen(state_tsr: Tensor) Tensor#
- Calculate Meyer-Wallach entanglement measure, proposed by Brennen. - See https://arxiv.org/pdf/quant-ph/0305094.pdf Eq.(6) - 备注 - This implementation is slower than - meyer_wallach_measurewhen \(\text{nqubit} \ge 8\).- 参数:
- state_tsr (torch.Tensor) – Input with the shape of \((\text{batch}, 2, ..., 2)\). 
- 返回:
- The value of Meyer-Wallach measure. 
- 返回类型:
- torch.Tensor 
 
deepquantum.qpd module#
Quasiprobability-decomposition gates
- class deepquantum.qpd.SingleGateQPD(bases: List[Tuple[Sequential, ...]], coeffs: List[float], label: int | None = None, name: str | None = None, nqubit: int = 1, wires: List[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - GateQPD- A base class for single-qubit QPD gates. - 参数:
- bases (List[Tuple[nn.Sequential, ...]]) – A list of tuples describing the operations probabilistically used to simulate an ideal quantum operation. 
- coeffs (List[float]) – The coefficients for quasiprobability representation. 
- label (int or None, optional) – The label of the gate. Default: - None
- name (str or None, optional) – The name of the quantum operation. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 1 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
- class deepquantum.qpd.DoubleGateQPD(bases: List[Tuple[Sequential, ...]], coeffs: List[float], label: int | None = None, name: str | None = None, nqubit: int = 2, wires: List[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 基类: - GateQPD- A base class for two-qubit QPD gates. - 参数:
- bases (List[Tuple[nn.Sequential, ...]]) – A list of tuples describing the operations probabilistically used to simulate an ideal quantum operation. 
- coeffs (List[float]) – The coefficients for quasiprobability representation. 
- label (int or None, optional) – The label of the gate. Default: - None
- name (str or None, optional) – The name of the quantum operation. Default: - None
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 - decompose() Tuple[SingleGateQPD, SingleGateQPD]#
- Decompose the gate into two single-qubit QPD gates. 
 
- class deepquantum.qpd.MoveQPD(nqubit: int = 2, wires: List[int] | None = None, label: int | None = None, den_mat: bool = False, tsr_mode: bool = False)#
- 
QPD representation of the move operation. - 参数:
- nqubit (int, optional) – The number of qubits that the quantum operation acts on. Default: 2 
- wires (List[int] or None, optional) – The indices of the qubits that the quantum operation acts on. Default: - None
- label (int or None, optional) – The label of the gate. Default: - None
- den_mat (bool, optional) – Whether the quantum operation acts on density matrices or state vectors. Default: - False(which means state vectors)
- tsr_mode (bool, optional) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: - False
 
 
deepquantum.state module#
Quantum states
- class deepquantum.state.QubitState(nqubit: int = 1, state: Any = 'zeros', den_mat: bool = False)#
- 基类: - Module- A quantum state of n qubits, including both pure states and density matrices. - 参数:
- nqubit (int, optional) – The number of qubits in the state. Default: 1 
- state (Any, optional) – The representation of the state. It can be one of the following strings: - 'zeros',- 'equal',- 'entangle',- 'GHZ', or- 'ghz'. Alternatively, it can be a tensor that represents a custom state vector or density matrix. Default:- 'zeros'
- den_mat (bool, optional) – Whether the state is a density matrix or not. Default: - False
 
 - to(arg: Any) QubitState#
- Set dtype or device of the - QubitState.
 - forward() None#
- Pass. 
 
- class deepquantum.state.MatrixProductState(nsite: int = 1, state: str | List[Tensor] | List[int] = 'zeros', chi: int | None = None, qudit: int = 2, normalize: bool = True)#
- 基类: - Module- A matrix product state (MPS) for quantum systems. - A matrix product state is a way of representing a quantum state as a product of local tensors. Each tensor has one physical index and one or two bond indices. The physical index corresponds to the local Hilbert space dimension of the qudit, while the bond indices correspond to the entanglement between qudits. - 参数:
- nsite (int, optional) – The number of sites of the MPS. Default: 1 
- state (str, List[torch.Tensor] or List[int], optional) – The representation of the MPS. If - 'zeros'or- 'vac', the MPS is initialized to the all-zero state. If a list of tensors, the MPS is initialized to the given tensors. The tensors must have the correct shape and dtype. If a list of integers, the MPS is initialized to the corresponding basis state. Default:- 'zeros'
- chi (int or None, optional) – The maximum bond dimension of the MPS. Default: 10 * - nsite
- qudit (int, optional) – The local Hilbert space dimension of each qudit. Default: 2 
- normalize (bool, optional) – Whether to normalize the MPS after each operation. Default: - True
 
 - to(arg: Any) MatrixProductState#
- Set dtype or device of the - MatrixProductState.
 - property tensors: List[Tensor]#
- Get the tensors of the matrix product state. - 备注 - This output is provided for reading only. Please modify the tensors through buffers. 
 - set_tensors(state: str | List[Tensor] | List[int]) None#
- Set the tensors of the matrix product state. 
 - center_orthogonalization(c: int, dc: int = -1, normalize: bool = False) None#
- Get the center-orthogonalization form of the MPS with center - c.
 - check_center_orthogonality(prt: bool = False) List[Tensor]#
- Check if the MPS is in center-orthogonal form. 
 - full_tensor() Tensor#
- Get the full tensor product of the state. 
 - inner(tensors: List[Tensor] | MatrixProductState, form: str = 'norm') Tensor | List[Tensor]#
- Get the inner product with another matrix product state. 
 - normalize_central_tensor() None#
- Normalize the center tensor. 
 - orthogonalize_left2right(site: int, dc: int = -1, normalize: bool = False) None#
- Orthogonalize the tensor at - siteand update the next one at- site+ 1.- It uses the QR decomposition or SVD, i.e., \(T = UR\) for the QR decomposition and \(T = USV^{\dagger} = UR\) for SVD. The tensor at - siteis replaced by \(U\). The tensor at- site+ 1 is updated by \(R\).- 参数:
- site (int) – The site of tensor to be orthogonalized. 
- dc (int, optional) – Keep the first - dcsingular values after truncation. Default: -1 (which means no truncation)
- normalize (bool, optional) – Whether to normalize the tensor \(R\). Default: - False
 
 
 - orthogonalize_right2left(site: int, dc: int = -1, normalize: bool = False) None#
- Orthogonalize the tensor at - siteand update the next one at- site- 1.- It uses the QR decomposition or SVD, i.e., \(T^{\dagger} = QR\) for the QR decomposition, which gives \(T = R^{\dagger}Q^{\dagger} = LV^{\dagger}\), and \(T = USV^{\dagger} = LV^{\dagger}\) for SVD. The tensor at - siteis replaced by \(V^{\dagger}\). The tensor at- site- 1 is updated by \(L\).- 参数:
- site (int) – The site of tensor to be orthogonalized. 
- dc (int, optional) – Keep the first - dcsingular values after truncation. Default: -1 (which means no truncation)
- normalize (bool, optional) – Whether to normalize the tensor \(L\). Default: - False
 
 
 - orthogonalize_n1_n2(n1: int, n2: int, dc: int, normalize: bool) None#
- Orthogonalize the MPS from site - n1to site- n2.
 - apply_mpo(mpo: List[Tensor], sites: List[int]) None#
- Use TEBD algorithm to contract tensors (contract local states with local operators), i.e., - >>> a >>> | >>> i-----O-----j a >>> | -> | >>> b ik---X---jl >>> | >>> k-----T-----l 
 - forward() None#
- Pass. 
 
- class deepquantum.state.DistributedQubitState(nqubit: int)#
- 基类: - Module- A quantum state of n qubits distributed between w nodes. - 参数:
- nqubit (int) – The number of qubits in the state. 
 - to(arg: Any) DistributedQubitState#
- Set dtype or device of the - DistributedQubitState.
 - reset()#
- Reset the state to the vacuum state. 
 
deepquantum.utils module#
Utilities
- deepquantum.utils.record_time(func: Callable) Callable#
- A decorator that records the running time of a function. 
- class deepquantum.utils.Time#
- 基类: - object- A decorator that records the running time of a function. 
Module contents#
This is the top level module from which all basic functions and classes of DeepQuantum can be directly imported.