algorithms

Contents

6.1. algorithms#

class InversePCCM(name, nameSpacePrefix='')[source]#

Bases: QuantumOperation

This operation corresponds to the inverse of the phase-covariant cloning machine with a specified angle.

The inverse can be obtained with `getInverse()` and it returns an object of type `PCCM`.

For more information, please refer to the documentation of the class `PCCM`.

getEquivalentSequence()[source]#

Return an object of the class `Sequence`, which contains a sequence of qubit swaps. The sequence of controlled and uncontrolled rotations correspond to the InversePCCM.

Returns:

sequence – An object of the class `Sequence` with controlled and uncontrolled rotation operations that correspond to the InversePCCM.

Return type:

geqo.core.Sequence

getInverse()[source]#

Return an object of the class `PCCM` with the same name for the rotation angle and the same name space prefix.

Returns:

PCCM – A new object of this class, which corresponds to the PCCM.

Return type:

geqo.algorithm.PCCM

getNumberClassicalBits()[source]#
Returns:

0 – The number of classical bits, which are used by the InversePCCM, is zero.

Return type:

int

getNumberQubits()[source]#

Return the number of qubits that are used by this InversePCCM operation.

Returns:

2 – The number of qubits, which are used by the InversePCCM.

Return type:

int

hasDecomposition()[source]#
Returns:

True – The InversePCCM can be decomposed into a sequence of rotation gates.

Return type:

Bool

isUnitary()[source]#
Returns:

True – This is a unitary operation.

Return type:

Bool

class InverseQFT(numberQubits, nameSpacePrefix='')[source]#

Bases: QuantumOperation

This operation corresponds to the inverse of the quantum Fourier transform on a specified number of qubits.

The inverse can be obtained with `getInverse()` and it returns an object of type `QFT`.

For more information, please refer to the documentation of the class `QFT`.

getEquivalentSequence()[source]#

Return an object of the class `Sequence`, which contains a sequence of Hadamard and controlled phase gates. The sequence of gates correspond to the InverseQFT.

Returns:

sequence – An object of the class `Sequence` with Hadamard and controlled phase operations and the appropriate bits and qubits.

Return type:

geqo.core.Sequence

getInverse()[source]#

Return an object of the class `QFT` with the same number of qubits and the same name space prefix.

Returns:

QFT – A new object of this class, which corresponds to the QFT.

Return type:

geqo.algorithm.QFT

getNumberClassicalBits()[source]#
Returns:

0 – The number of classical bits, which are used by InverseQFT, is zero.

Return type:

int

getNumberQubits()[source]#

Return the number of qubits that are used by this InverseQFT operation.

Returns:

numberQubits – The number of qubits, which are used by this InverseQFT operation.

Return type:

int

hasDecomposition()[source]#
Returns:

True – The InverseQFT can be decomposed into a sequence of Hadamard gates and controlled phase gates and a qubit reversal.

Return type:

Bool

isUnitary()[source]#
Returns:

True – This is a unitary operation.

Return type:

Bool

class PCCM(name, nameSpacePrefix='')[source]#

Bases: QuantumOperation

The circuit for a phase-covariant cloning machine with one free parameter, which is the angle of a controlled rotation in its circuit representation.

The inverse can be obtained with `getInverse()` and it returns an object of type `InversePCCM`.

The circuit diagram can be found in figure 3 of T. Decker, M. Gallezot, S. F. Kerstan, A. Paesano, A. Ginter, W. Wormsbecher, “QKD as a Quantum Machine Learning task”, arXiv:2410.01904

getEquivalentSequence()[source]#

Return an object of the class `Sequence`, which contains a sequence of qubit swaps. The sequence of controlled and uncontrolled rotations correspond to the PCCM.

Returns:

sequence – An object of the class `Sequence` with controlled and uncontrolled rotation operations that correspond to the PCCM.

Return type:

geqo.core.Sequence

getInverse()[source]#

Return an object of the class `InversePCCM` with the same name for the rotation angle and the same name space prefix.

Returns:

InversePCCM – A new object of this class, which corresponds to the inverse of the PCCM.

Return type:

geqo.algorithm.InversePCCM

getNumberClassicalBits()[source]#
Returns:

0 – The number of classical bits, which are used by the PCCM, is zero.

Return type:

int

getNumberQubits()[source]#

Return the number of qubits that are used by this PCCM operation.

Returns:

2 – The number of qubits, which are used by the PCCM.

Return type:

int

hasDecomposition()[source]#
Returns:

True – The PCCM can be decomposed into a sequence of rotation gates.

Return type:

Bool

isUnitary()[source]#
Returns:

True – This is a unitary operation.

Return type:

Bool

class Pattern(name, inputSequence, outputSequence, lambdas)[source]#

Bases: object

This class helps create a database of patterns, which are used as replacements for parts of a circuit to reduce circuit depth. The lambda function is necessary for assisiting the setValue method in generating replacements.

replace_recursion(pattern: QuantumOperation, substitutions: dict, substitutionsQ: dict, substitutionsC: dict)[source]#

This function replaces the pattern quantum operation with the quantum operation stored in substitutions.

Parameters:
  • pattern (A quantum operation in the pattern database.)

  • substitutions (A dictionary that stores the matching/unified parameter pairs.)

  • substitutionsQ (A dictionary that stores the matching/unified qubit pairs.)

  • substitutionsC (A dictionary that stores the matching/unified bit pairs.)

Returns:

QuantumOperation

Return type:

The output quantum operation with the replaced parameters.

replacer(target: Sequence)[source]#

This function transforms the output sequence of the Pattern instance by substituting its parameters based on the mapping from the input sequence to the target sequence.

Parameters:

target (The sequence the forms the matching parameter (and qubit) pair with the input sequence of the Pattern instance.)

Returns:

  • replacerPatternSubst (Updated output sequence with the replaced parameters.)

  • res2 (The mapping between the input sequence and the target sequence used for parameter substitution.)

unificator(pattern: QuantumOperation, target: QuantumOperation, alreadyDefined: dict, alreadyDefinedQubits: dict, alreadyDefinedBits: dict)[source]#

This function unifies/matches the parameters associated with the same quantum operation.

Parameters:
  • pattern (A quantum operation in the pattern database.)

  • target (A quantum operation in the target circuit.)

  • alreadyDefined (A dictionary that stores the mapping between pattern paramters and target parameters.)

  • alreadyDefinedQubits (A dictionary that stores the mapping between pattern qubits and target qubits.)

  • alreadyDefinedBits (A dictionary that stores the mapping between pattern bits and target bits.)

Returns:

  • True/False (whether the unification of the pattern and target quantum operations is possible.)

  • alreadyDefined (updated mapping of pattern and target parameters.)

  • alreadyDefinedQubits (updated mapping of pattern and target qubits.)

  • alreadyDefinedBits (updated mapping of pattern and target bits.)

class PermuteQubits(targetOrder)[source]#

Bases: QuantumOperation

This class allows to define a unitary operation that corresponds to a permutation of qubits. The argument is the target order of the permuted qubits, which are denoted by 0, …, n-1 for n qubits.

For instance, `PermuteQubits([2,1,0])` defines the bit reversal on three qubits.

getEquivalentSequence()[source]#

Return an object of the class `Sequence`, which does not contain any operators, because a permutation of qubits is considered to be as a non-decomposable operation.

Returns:

sequence – An object of the class `Sequence` without operations, but with the appropriate bits and qubits.

Return type:

geqo.core.Sequence

getInverse()[source]#

Return an object of the same class, but it corresponds to the inverse permutation of qubits as this object.

Returns:

PermuteQubits – A new object of this class, which corresponds to the inverse permutation of qubits.

Return type:

geqo.algorithm.PermuteQubits

getNumberClassicalBits()[source]#
Returns:

0 – The number of classical bits, which are used by PermuteQubits, is zero.

Return type:

int

getNumberQubits()[source]#

Return the number of qubits that are used by this permuation operation.

Returns:

numberQubits – The number of qubits, which are used by this permutation of qubits. This is equal to the length of the list representation of the permuation.

Return type:

int

hasDecomposition()[source]#
Returns:

False – A permutation of qubits is considered to be as a non-decomposable operation.

Return type:

Bool

isUnitary()[source]#
Returns:

True – This is a unitary operation.

Return type:

Bool

class QFT(numberQubits, nameSpacePrefix='')[source]#

Bases: QuantumOperation

This operation corresponds to the quantum Fourier transform on a specified number of qubits.

For the definition, see `https://en.wikipedia.org/wiki/Quantum_Fourier_transform`.

The inverse can be obtained with `getInverse()` and it returns an object of type `InverseQFT`.

The QFT can be decomposed into a sequence of controlled phase operations and Hadamard gates, see `https://en.wikipedia.org/wiki/Toffoli_gate#Related_logic_gates` for details.

This decomposition can be obtained with the function `getEquivalentSequence`. It returns a sequence of Hadamard gates and gates of the class Phase with the names `Ph1`, `Ph2, … where `Phk` corresponds to a phase gate with phase $e^{2pi i/2^k}.

For instance, the QFT on two qubits corresponds to the sequence `Sequence([], [0, 1], [(Hadamard(), [0]), (QuantumControl([1], Phase("Ph1")), [1, 0]), (Hadamard(), [1]), (QubitReversal(2), [0, 1])])` where `Ph1` denotes

To avoid conflicts with the names of other gates, a name space prefix can be provided to the contructor of QFT. For instance, the object `QFT(2, "test.")` leads to the sequence `Sequence([], [0, 1], [(Hadamard(), [0]), (QuantumControl([1], Phase("test.Ph1")), [1, 0]), (Hadamard(), [1]), (QubitReversal(2), [0, 1])])`.

For convenience, a backend might be prepared for applying a QFT with the function `prepareBackend` of the corresponding backend class.

getEquivalentSequence()[source]#

Return an object of the class `Sequence`, which contains a sequence of Hadamard and controlled phase gates. The sequence of gates correspond to the QFT.

Returns:

sequence – An object of the class `Sequence` with Hadamard and controlled phase operations and the appropriate bits and qubits.

Return type:

geqo.core.Sequence

getInverse()[source]#

Return an object of the class `InverseQFT` with the same number of qubits and the same name space prefix.

Returns:

InverseQFT – A new object of this class, which corresponds to the inverse QFT.

Return type:

geqo.algorithm.InverseQFT

getNumberClassicalBits()[source]#
Returns:

0 – The number of classical bits, which are used by the QFT, is zero.

Return type:

int

getNumberQubits()[source]#

Return the number of qubits that are used by this QFT operation.

Returns:

numberQubits – The number of qubits, which are used by this QFT operation.

Return type:

int

hasDecomposition()[source]#
Returns:

True – The QFT can be decomposed into a sequence of Hadamard gates and controlled phase gates and a qubit reversal.

Return type:

Bool

isUnitary()[source]#
Returns:

True – This is a unitary operation.

Return type:

Bool

class QubitReversal(numberQubits)[source]#

Bases: QuantumOperation

This operation is reversing the order of qubits. It corresponds to a permutation of the states of the computational basis.

See `https://en.wikipedia.org/wiki/Bit-reversal_permutation` for the related bit-reversal permutation.

getEquivalentSequence()[source]#

Return an object of the class `Sequence`, which contains a sequence of qubit swaps. The sequence of swaps correspond to the qubit reversal.

Returns:

sequence – An object of the class `Sequence` with swap operations and the appropriate bits and qubits.

Return type:

geqo.core.Sequence

getInverse()[source]#

Return this object because the reversal of qubits is inverse to itself.

Returns:

QubitReversal – Return the same object because it is self-inverse.

Return type:

geqo.algorithm.QubitReversal

getNumberClassicalBits()[source]#
Returns:

0 – The number of classical bits, which are used by this qubit reversal operation, is zero.

Return type:

int

getNumberQubits()[source]#

Return the number of qubits that are used by this qubit reversal operation.

Returns:

numberQubits – The number of qubits, which are used by this qubit reversal.

Return type:

int

hasDecomposition()[source]#
Returns:

True – The qubit reversal can be decomposed into a sequence of qubit swaps.

Return type:

Bool

isUnitary()[source]#
Returns:

True – This is a unitary operation.

Return type:

Bool

RiskModel(node_probs: dict, edge_probs: dict)[source]#

Creates a quantum circuit implementation of the business risk model defined by a probabilistic network. The underlying graph of the network has to be a directed acyclic graph.

Input:

node_probs: dictionary with the nodes as keys and their

intrinsic probabilities as values

edge_probs: dictionary with edges as keys and the corresponding

transition probabilities as values

Output:

Quantum circuit implementation of the network and dictionary to prepare the SymPy simulator.

controlledXGate(numberControls, namePrefix='')[source]#

Implement an `PauliX` gate, which is controlled by multiple qubits. It is implemented with Toffoli gates and one ancilla qubit. This method creates an exponential number of Toffoli gates in the number of control qubits. This function needs one ancilla qubit, which is assumed to be the last one in the order.

Parameters:
  • numberControls (int) – The number of control qubits for this gate.

  • namePrefix (string) – This character string is prepended to all internally used definitions.

Returns:

  • toffoli (geqo.gates.multi_qubit_gates.Toffoli) – An object of the class `Toffoli` that is used in the decomposition. Its internal name space prefix is set to the given prefix.

  • controlledXGateInternal (geqo.core.quantum_circuit.Sequence) – A sequence with gates that correspond to the controlled `PauliX` gate.

findLongestRunSequence(seq: Sequence, start: int, qubits: List, verbose: bool = False)[source]#

This function scans through the given sequnce and picks out gates whose qubits are: 1. among the specified target qubits and 2. not among the qubits involved with the previous operations that contain a non-specified qubit. Ex.1 Given the Sequence([],[“0”,”1”,”2”],[ (Hadamard(),[“0”]), (Hadamard(), [“1”]), (CNOT(),[“1”,”2”]), (Hadamard(),[“0”])]) and the specified target qubits [“0”,”1”], the function picks out [(Hadamard(), [‘0’]), (Hadamard(), [‘1’]), (Hadamard(), [‘0’])] Ex.2 Given the Sequence([],[0,1,2,3],[ (Hadamard(),[0]), (CNOT(),[1,2]), (CNOT(),[2,3]), (CNOT(),[0,2]), (Hadamard(),[0]), (Hadamard(),[1])]) and the specified target qubits [0,1,2], the function picks out [(Hadamard(), [0]), (CNOT(), [1, 2]), (Hadamard(), [1])], since (CNOT(),[2,3]) contains the non-specified qubit 3, and (CNOT(),[0,2]), (Hadamard(),[0]) both contain a qubit (2 and 0) that is involved with the previous operation that have a non-specified qubit 3.

Parameters:
  • seq (The sequence to be scanned.)

  • start (The starting index of the scan.)

  • qubits (The specified target qubits.)

  • verbose (Whether to display scan messages.)

Returns:

  • Sequence (The sequence containig the selected gates.)

  • gatheredIndices (The indices of the selected gates in the scanned sequence.)

getAllLongestRuns(sequence: Sequence, numberQubits: int, verbose: bool = False)[source]#

This function scans through the whole sequence in search of potential patterns. The search covers all possible numberQubits-combinations of target qubits and progressively accumulates the longest valid subsequences from the beginning of the sequence.

Parameters:
  • sequence (The sequence to be scanned.)

  • numberQuibits (The number of qubits in a combination.)

  • verbose (Whether to display scan messages.)

Returns:

  • candidates (All valid qubit combinations and gate indices that could potentially become patterns.)

  • gatheredIndices (The indices of the selected gates in the scanned sequence.)

getAllRuns(sequence: Sequence, numberQubits: int, numberGates: int, verbose: bool = False)[source]#

This function resembles the getAllLongestRuns function, except that it imposes a length constraint numberGates on the accumulated subsequences.

Parameters:
  • sequence (The sequence to be scanned.)

  • numberQuibits (The number of qubits in a combination.)

  • numberGates (The number of gates in a subsequence.)

  • verbose (Whether to display scan messages.)

Returns:

newCandidates

Return type:

The updated list of candidates that contain exactly numberGates gates in the subsequences.

unitaryDecomposer(u, decompose_givens: bool = False)[source]#

This function decomposes an arbitrary unitary matrix u into an equivalent sequence with Toffoli, controlled-rotation, and controlled-phase gates

Parameters:
  • u (The unitary matrix to be decomposed.)

  • decompose_givens (Whether to decompose the Givens rotation into Ry Rz gates.)

Returns:

  • seq (The decomposed Sequence instance.)

  • params (A dictionary storing the parameter values of the sequence.)