API#

Photonic#

Submodules#

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 wires is 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 a in \(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.

a has a modular inverse only if a is 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 a in \(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 a in \(\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 a in \(\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.Parameter or 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 wires is 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.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)#

基类:Operation

Quantum circuit for qubits.

This class inherits from the Operation class 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

抛出:

AssertionError – If the type or dimension of init_state does not match nqubit or den_mat.

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 operators of the quantum circuit to the initial state or the given state and returns the resulting state. If data is given, it is used as the input for the encoders. The state can be either a MatrixProductState or a QubitState object, or a tensor representation of them. The data must 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 encoders of the quantum circuit and initializes their parameters with the input data. If reupload is False, the input data must be at least as long as the number of parameters in the encoders. If reupload is 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 reupload is False and 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(init_state: Any = 'zeros') None#

Reset the QubitCircuit according 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 = 1024, with_prob: bool = False, wires: int | List[int] | None = None) Dict | List[Dict] | None#

Measure the final state.

expectation() Tensor#

Get the expectation value according to the final state and observables.

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.

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 encode to be True.

property max_depth: int#

Get the max number of gates on the wires.

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 wires is specified, the parameters of gates are shared.

参数:
  • op (Operation) – The operation to add. It is an instance of Operation class or its subclasses, such as Gate, Layer, or QubitCircuit.

  • encode (bool) – Whether the gate or layer is to encode data. Default: False

  • wires (Union[int, List[int], None]) – 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 (Union[int, List[int], None]) – 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 wires is 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.

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.

barrier(wires: int | List[int] | None = None) None#

Add barrier.

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 controls as 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.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 controls as 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 controls as 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 wires is 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 controls as 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.Parameter or 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.

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 controls as 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.Parameter or 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.

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)#

基类:ParametricSingleGate

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 controls as 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.Parameter or 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.

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)#

基类:ParametricSingleGate

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 controls as 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.Parameter or 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 controls as 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.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 controls as 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.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 controls as 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.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 controls as 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.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 controls as 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.

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 controls as 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() SGate#

Get the inversed gate.

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 controls as 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 controls as 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() TGate#

Get the inversed gate.

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)#

基类:ParametricSingleGate

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 controls as 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.Parameter or buffer. Default: False (which means buffer)

get_matrix(theta: Any) Tensor#

Get the local unitary matrix.

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)#

基类:ParametricSingleGate

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 controls as 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.Parameter or buffer. Default: False (which means buffer)

get_matrix(theta: Any) Tensor#

Get the local unitary matrix.

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)#

基类:ParametricSingleGate

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 controls as 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.Parameter or buffer. Default: False (which means buffer)

get_matrix(theta: Any) Tensor#

Get the local unitary matrix.

class deepquantum.gate.CombinedSingleGate(gatelist: 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.

参数:
  • gatelist (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 controls as 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.

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)#

基类:DoubleControlGate

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

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 controls as 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.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)#

基类:ParametricDoubleGate

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 controls as 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.Parameter or 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)#

基类:ParametricDoubleGate

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 controls as 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.Parameter or 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)#

基类:ParametricDoubleGate

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 controls as 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.Parameter or 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)#

基类:ParametricDoubleGate

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 controls as 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.Parameter or 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)#

基类:ParametricDoubleGate

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 controls as 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.Parameter or 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.

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)#

基类:ArbitraryGate

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 wires is 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)#

基类:ArbitraryGate

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 wires is 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.Parameter or 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.

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)#

基类:ArbitraryGate

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 wires or minmax is 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 hamiltonian is 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 hamiltonian is not a list and wires is 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.Parameter or buffer. Default: False (which means buffer)

get_minmax(hamiltonian: List) List[int]#

Get minmax according 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.

init_para(inputs: List | None = None) None#

Initialize the parameters.

class deepquantum.gate.Barrier(nqubit: int = 1, wires: int | List[int] | None = None)#

基类: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

forward(x: Any) Any#

Perform a forward pass.

deepquantum.layer module#

Quantum layers

class deepquantum.layer.SingleLayer(name: str | None = None, nqubit: int = 1, wires: int | List[List[int]] | 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[List[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.

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[List[int]] | List[int] | None = None, basis: str = 'z', den_mat: bool = False, tsr_mode: bool = False)#

基类:SingleLayer

A Layer that 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[List[int]] | List[int] | None = None, inputs: Any | None = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)#

基类:SingleLayer

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.Parameter or buffer. Default: True (which means nn.Parameter)

inverse() U3Layer#

Get the inversed layer.

class deepquantum.layer.XLayer(nqubit: int = 1, wires: int | List[List[int]] | 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[List[int]] | 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[List[int]] | 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[List[int]] | 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[List[int]] | List[int] | None = None, inputs: Any | None = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)#

基类:SingleLayer

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.Parameter or buffer. Default: True (which means nn.Parameter)

inverse() RxLayer#

Get the inversed layer.

class deepquantum.layer.RyLayer(nqubit: int = 1, wires: int | List[List[int]] | List[int] | None = None, inputs: Any | None = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)#

基类:SingleLayer

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.Parameter or buffer. Default: True (which means nn.Parameter)

inverse() RyLayer#

Get the inversed layer.

class deepquantum.layer.RzLayer(nqubit: int = 1, wires: int | List[List[int]] | List[int] | None = None, inputs: Any | None = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)#

基类:SingleLayer

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.Parameter or buffer. Default: True (which means nn.Parameter)

inverse() RzLayer#

Get the inversed layer.

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

inverse() CnotLayer#

Get the inversed layer.

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

  • wires (List[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 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 | 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 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.

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 controls as 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

update_matrix() Tensor#

Update 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.

forward(x: Tensor | MatrixProductState) Tensor | MatrixProductState#

Perform a forward pass.

inverse() Gate#

Get the inversed gate.

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 | 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 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.

forward(x: Tensor | MatrixProductState) Tensor | MatrixProductState#

Perform a forward pass.

inverse() Layer#

Get the inversed gate.

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 kwargs in the format of param_init as 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 kwargs in the format of param_init as 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 kwargs in the format of param_init as 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 kwargs in the format of param_init as 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.qmath module#

Common functions

deepquantum.qmath.is_power_of_two(n: int) bool#

Check if an integer is power of two.

deepquantum.qmath.int_to_bitstring(x: int, n: int, debug: bool = False) str#

Convert from integer to bit string.

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.

返回:

True if matrix is unitary, False otherwise.

返回类型:

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.

返回:

True if the tensor is a density matrix, False otherwise.

返回类型:

bool

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 ctx object. output is the output of the forward, inputs are 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:

  1. Override forward with the signature forward(ctx, *args, **kwargs). setup_context is not overridden. Setting up the ctx for backward happens inside the forward.

  2. 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 vjp function.)

It must accept a context ctx as 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_grad as a tuple of booleans representing whether each input needs gradient. E.g., backward() will have ctx.needs_input_grad[0] = True if 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 ctx object. output is the output of the forward, inputs are 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:

  1. Override forward with the signature forward(ctx, *args, **kwargs). setup_context is not overridden. Setting up the ctx for backward happens inside the forward.

  2. 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 vjp function.)

It must accept a context ctx as 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_grad as a tuple of booleans representing whether each input needs gradient. E.g., backward() will have ctx.needs_input_grad[0] = True if 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.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, nqubit: int, trace_lst: List[int]) Tensor#

Calculate the partial trace for a batch of density matrices.

参数:
  • rho (torch.Tensor) – Density matrices with the shape of \((\text{batch}, 2^{\text{nqubit}}, 2^{\text{nqubit}})\).

  • nqubit (int) – Total number of qubits.

  • trace_lst (List[int]) – A list of qubits to be traced.

返回:

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.measure(state: Tensor, shots: int = 1024, with_prob: bool = False, wires: int | List[int] | None = None, den_mat: bool = False) 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

返回:

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.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 Observable class 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.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 j th 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 j th 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_measure when \(\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.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

forward() None#

Pass.

class deepquantum.state.MatrixProductState(nsite: int = 1, state: str | List[Tensor] = '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 or List[torch.Tensor], optional) – The representation of the MPS. If 'zeros', 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. 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

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(tensors: List[Tensor]) 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 site and 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 site is 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 dc singular 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 site and 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 site is 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 dc singular 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 n1 to 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.

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.