ansatz#

Ansatze: various quantum circuits

Classes

Ansatz(nqubit[, wires, minmax, ancilla, ...])

A base class for Ansatz.

ControlledMultiplier(nqubit, a, mod[, ...])

Controlled multiplier.

ControlledUa(nqubit, a, mod[, minmax, ...])

Controlled Ua gate.

HHL(ncount, mat[, t0, den_mat, mps, chi, ...])

A quantum circuit for the HHL algorithm.

NumberEncoder(nqubit, number[, minmax, ...])

Convert number to corresponding encoding circuit.

PhiAdder(nqubit, number[, minmax, controls, ...])

Phi adder.

PhiModularAdder(nqubit, number, mod[, ...])

Phi modular adder.

QuantumConvolutionalNeuralNetwork(nqubit, nlayer)

Quantum convolutional neural network.

QuantumFourierTransform(nqubit[, minmax, ...])

Quantum Fourier transform.

QuantumPhaseEstimation(nqubit, ncount, unitary)

Quantum phase estimation for arbitrary unitary operator.

QuantumPhaseEstimationSingleQubit(t, phase)

Quantum phase estimation for single-qubit gate.

RandomCircuitG3(nqubit, ngate[, wires, ...])

Random circuit of G3 family.

ShorCircuit(mod, ncount, a[, den_mat, mps, ...])

Circuit for Shor's algorithm.

ShorCircuitFor15(ncount, a[, den_mat, mps, chi])

Circuit for Shor's algorithm to factor number 15.

class 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)[source]#

Bases: QubitCircuit

A base class for Ansatz.

Parameters:
  • nqubit (int) – The number of qubits in the circuit.

  • wires (int | list[int] | None) – The indices of the qubits that the quantum operation acts on. Default: None

  • minmax (list[int] | None) – 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] | None) – The indices of the ancilla qubits. Default: None

  • controls (int | list[int] | None) – The indices of the control qubits. Default: None

  • init_state (Any) – The initial state of the circuit. Default: 'zeros'

  • name (str | None) – The name of the circuit. Default: None

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • reupload (bool) – Whether to use data re-uploading. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

class 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)[source]#

Bases: Ansatz

Controlled multiplier.

See https://arxiv.org/pdf/quant-ph/0205095.pdf Fig.6

Parameters:
  • 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] | None) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: None

  • ancilla (int | list[int] | None) – The indices of the ancilla qubits. Default: None

  • nqubitx (int | None) – The number of qubits in the register x.

  • controls (int | list[int] | None) – The indices of the control qubits. Default: None

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

  • debug (bool) – Whether to print the debug information. Default: False

class 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)[source]#

Bases: 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

Parameters:
  • 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] | None) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: None

  • ancilla (int | list[int] | None) – The indices of the ancilla qubits. Default: None

  • controls (int | list[int] | None) – The indices of the control qubits. Default: None

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

  • debug (bool) – Whether to print the debug information. Default: False

class HHL(ncount: int, mat: Any, t0: float = 1, den_mat: bool = False, mps: bool = False, chi: int | None = None, show_barrier: bool = False)[source]#

Bases: Ansatz

A quantum circuit for the HHL algorithm.

Parameters:
  • ncount (int) – The number of counting qubits.

  • mat (Any) – The Hermitian matrix A.

  • t0 (float) – The time parameter for the matrix exponential in units of \(2\pi\). Default: 1

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

  • show_barrier (bool) – Whether to show the barriers in the circuit. Default: False

class NumberEncoder(nqubit: int, number: int, minmax: list[int] | None = None, den_mat: bool = False, mps: bool = False, chi: int | None = None)[source]#

Bases: Ansatz

Convert number to corresponding encoding circuit.

Parameters:
  • nqubit (int) – The number of qubits in the circuit.

  • number (int) – The integer for converting to bit string.

  • minmax (list[int] | None) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: None

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

class 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)[source]#

Bases: Ansatz

Phi adder.

See https://arxiv.org/pdf/quant-ph/0205095.pdf Fig.2 and Fig.3

Parameters:
  • nqubit (int) – The number of qubits in the circuit.

  • number (int) – Number a in \(\Phi(a+b)\).

  • minmax (list[int] | None) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: None

  • controls (int | list[int] | None) – The indices of the control qubits. Default: None

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

  • debug (bool) – Whether to print the debug information. Default: False

class 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)[source]#

Bases: Ansatz

Phi modular adder.

See https://arxiv.org/pdf/quant-ph/0205095.pdf Fig.5

Parameters:
  • 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] | None) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: None

  • ancilla (int | list[int] | None) – The indices of the ancilla qubits. Default: None

  • controls (int | list[int] | None) – The indices of the control qubits. Default: None

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

  • debug (bool) – Whether to print the debug information. Default: False

class 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)[source]#

Bases: Ansatz

Quantum convolutional neural network.

See https://readpaper.com/paper/4554418257818296321 Fig.1 or https://pennylane.ai/qml/demos/tutorial_learning_few_data

Parameters:
  • nqubit (int) – The number of qubits in the circuit.

  • nlayer (int) – The number of layers.

  • minmax (list[int] | None) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: None

  • init_state (Any) – The initial state of the circuit. Default: 'zeros'

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • requires_grad (bool) – Whether the parameters are nn.Parameter or buffer. Default: True (which means nn.Parameter)

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

conv(wires: list[int]) None[source]#
pool(wires: list[int]) None[source]#
class 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)[source]#

Bases: Ansatz

Quantum Fourier transform.

Parameters:
  • nqubit (int) – The number of qubits in the circuit.

  • minmax (list[int] | None) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: None

  • reverse (bool) – 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) – The initial state of the circuit. Default: 'zeros'

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

  • show_barrier (bool) – Whether to show the barriers in the circuit. Default: False

qft_block(n: int) None[source]#
class 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)[source]#

Bases: Ansatz

Quantum phase estimation for arbitrary unitary operator.

Parameters:
  • nqubit (int) – The number of qubits in the circuit.

  • ncount (int) – The number of counting qubits.

  • unitary (Any) – The unitary operator.

  • minmax (list[int] | None) – The minimum and maximum indices of the qubits that the quantum operation acts on. Default: None

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

  • show_barrier (bool) – Whether to show the barriers in the circuit. Default: False

class QuantumPhaseEstimationSingleQubit(t: int, phase: Any, den_mat: bool = False, mps: bool = False, chi: int | None = None)[source]#

Bases: Ansatz

Quantum phase estimation for single-qubit gate.

Parameters:
  • t (int) – The number of counting qubits.

  • phase (Any) – The phase to be estimated.

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

class 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)[source]#

Bases: Ansatz

Random circuit of G3 family.

Parameters:
  • nqubit (int) – The number of qubits in the circuit.

  • ngate (int) – The number of random gates in the circuit.

  • wires (list[int] | None) – The indices of the qubits that the random gates act on. Default: None

  • minmax (list[int] | None) – 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) – The initial state of the circuit. Default: 'zeros'

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

class ShorCircuit(mod: int, ncount: int, a: int, den_mat: bool = False, mps: bool = False, chi: int | None = None, debug: bool = False)[source]#

Bases: Ansatz

Circuit for Shor’s algorithm.

Parameters:
  • 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) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

  • debug (bool) – Whether to print the debug information. Default: False

class ShorCircuitFor15(ncount: int, a: int, den_mat: bool = False, mps: bool = False, chi: int | None = None)[source]#

Bases: Ansatz

Circuit for Shor’s algorithm to factor number 15.

See https://learn.qiskit.org/course/ch-algorithms/shors-algorithm

Parameters:
  • ncount (int) – The number of counting qubits.

  • a (int) – Any integer that satisfies \(1 < a < N\) and \(\gcd(a, N) = 1\).

  • den_mat (bool) – Whether to use density matrix representation. Default: False

  • mps (bool) – Whether to use matrix product state representation. Default: False

  • chi (int | None) – The bond dimension for matrix product state representation. Default: None

cua(a: int, power: int, controls: int | list[int] | None) None[source]#