qcp.algorithms package

Submodules

qcp.algorithms.abstract_algorithm module

Constructs the quantum register, circuits of composite gates, and runs the simulation of Grover’s Algorithm

class qcp.algorithms.abstract_algorithm.GeneralAlgorithm(size: int)[source]

Bases: abc.ABC

__init__(size: int)[source]
_abc_impl = <_abc._abc_data object>
construct_circuit() qcp.matrices.matrix.Matrix[source]

Construct the circuit for the algorithm

Returns

Matrix representing our the circuit for the algorithm

Return type

Matrix

initial_state() qcp.matrices.matrix.Matrix[source]

Creates a state vector corresponding to |1..0>

Returns

the state vector

Return type

Matrix

measure() Tuple[int, float][source]

‘measures’ self.state by selecting a state weighted by its (amplitude ** 2)

Returns

The state observed and the probability of measuring said state

Return type

Tuple[int, float]

measure_probabilities()[source]

Print a table of the probabilities associated with each measured state.

probabilities() List[float][source]

Returns the amplitudes of the measured state, representing the probabilities to be in each state.

Returns

A list of states, where each element is the

probability to be in that state.

Return type

List[float]

run() qcp.matrices.matrix.Matrix[source]

Run the algorithm by applying the quantum circuit to the initial state

Returns

Column matrix representation of the final state

Return type

Matrix

qcp.algorithms.grovers_algorithm module

Constructs the quantum register, circuits of composite gates, and runs the simulation of Grover’s Algorithm

class qcp.algorithms.grovers_algorithm.Grovers(size: int, target_state: int)[source]

Bases: qcp.algorithms.abstract_algorithm.GeneralAlgorithm

__init__(size: int, target_state: int)[source]

This is an implementation of Grover’s algorithm which efficiently finds a specific item in a list of items. In this implementation we use Grover’s algorithm to increase the amplitude of the “target_state” and reduce all others in a “size”-qubit system

Parameters
  • size (int) – number of qubits in our circuit

  • target_state (int) – specific state we want to target/select

_abc_impl = <_abc._abc_data object>
construct_circuit() qcp.matrices.matrix.Matrix[source]

Constructs the circuit for Grover’s algorithm by applying an initial set of Hadamards and repeating the oracle and diffusion gates until our target state is close to 1 in terms of probability

Returns

Matrix representing our completed Grover’s algorithm

Return type

Matrix

diffusion() qcp.matrices.matrix.Matrix[source]

Creates a diffusion gate - a gate which amplifies the probability of selecting our target state

Returns

Matrix representing diffusion gate

Return type

Matrix

measure_probabilities()[source]

Print a table of the probabilities associated with each measured state.

single_target_oracle() qcp.matrices.matrix.Matrix[source]

Creates an oracle gate - a gate which ‘selects’ our target state by phase shifting it by pi (turning 1 into -1 in the matrix representation)

Returns

Matrix representation of our Oracle

Return type

Matrix

qcp.algorithms.grovers_algorithm.pull_set_bits(n: int) List[int][source]

Creates a list of bits that would be set to 1 to make the number n

Parameters

n (int) – number

Returns

list of bits

Return type

List[int]

qcp.algorithms.phase_estimation module

class qcp.algorithms.phase_estimation.PhaseEstimation(size: int, unitary: qcp.matrices.matrix.Matrix, eigenvector: qcp.matrices.matrix.Matrix)[source]

Bases: qcp.algorithms.abstract_algorithm.GeneralAlgorithm

__init__(size: int, unitary: qcp.matrices.matrix.Matrix, eigenvector: qcp.matrices.matrix.Matrix)[source]

Implement Phase Estimation, which requires a unitary matrix and one of its eigenvector as the input.

Parameters
  • int – size: precision of the phase output, i.e. no. of decimal

  • unitary (Matrix) – a unitary matrix whose eigenvalue’s phase is the target

  • eigenvector (Matrix) – an eigenvector of the unitary matrix

Example: phase = 0.125 unitary = DefaultMatrix([[1,0],[0,exp(1j*math.pi*phase)]]) eigenvector = DefaultMatrix([[0],[1]]) PE = Phase_Esimation(3,unitary,eigenvector) PE.run() print(PE.measure())

_abc_impl = <_abc._abc_data object>
construct_circuit()[source]

Combines the layers

first_layer() qcp.matrices.matrix.Matrix[source]

Tensor Hadamard (first register) with Identity (auxiliary)

initial_state() qcp.matrices.matrix.Matrix[source]

Creates a state vector corresponding to |0..0> :return: returns state vector

measure_phase() float[source]

Measure the calculated phase corresponding to the state measured

Returns

The calculated phase

Return type

float

measure_probabilities()[source]

Print a table of the probabilities associated with each measured state.

probabilities() List[float][source]

Returns the amplitudes of the measured state, representing the probabilities to be in each state.

Returns

A list of states, where each element is the

probability to be in that state.

Return type

List[float]

second_layer() qcp.matrices.matrix.Matrix[source]

Control-U Gate applied to auxiliary register

third_layer()[source]

” Inverse QFT Gate tensor for the first register

qcp.algorithms.phase_estimation.inverse_qft_gate(size: int) qcp.matrices.matrix.Matrix[source]

Performs Inverse Quantum Fourier Transform :param int: size: number of qubits :returns: gate :rtype: Matrix

qcp.algorithms.phase_estimation.inverse_qft_rotation_gate(size: int, current_qubit: int) qcp.matrices.matrix.Matrix[source]

” Construct the R2…R(n-i) gate for Inverse Quantum Fourier Transform

Parameters
  • size (int) – total number of qubits, n

  • current_qubit (int) – which qubit to apply the rotation gate to, i

qcp.algorithms.phase_estimation.optimum_qubit_size(precision: int, error: float) int[source]

Return the number of qubit required for targeted number of decimal and error rate.

qcp.algorithms.phase_estimation.qft_gate(size: int) qcp.matrices.matrix.Matrix[source]

Performs Quantum Fourier Transform, which change the basis

Parameters

int – size: number of qubits

Returns

gate

Return type

Matrix

qcp.algorithms.phase_estimation.qft_rotation_gate(size: int, current_qubit: int) qcp.matrices.matrix.Matrix[source]

” Construct the R2…R(n-i) gate for Quantum Fourier Transform

Parameters
  • size (int) – total number of qubits, n

  • intcurrent_qubit – which qubit to apply the rotation gate to, i

Returns

gate

Return type

Matrix

qcp.algorithms.phase_estimation_unitary_matrices module

Defines the enum that encodes the predefined Unitary matrices

class qcp.algorithms.phase_estimation_unitary_matrices.UnitaryMatrices(value)[source]

Bases: enum.Enum

Enum of all the available unitary matrices to use in the Phase Estimation algorithm

HADAMARD = 'hadamard'
PHASE_SHIFT = 'phase_shift'
basis() Tuple[qcp.matrices.matrix.Matrix, qcp.matrices.matrix.Matrix][source]

Return the basis used for the Unitary matrix choice

Returns

A two entry tuple of the 2 qbit

basis.

Return type

Tuple[Matrix, Matrix]

basis_names() Tuple[str, str][source]

Return the basis names used for the Unitary matrix choice

Returns

A two entry tuple of the 2 qbit

basis names.

Return type

Tuple[str, str]

get(val1: float = 0.0, val2: float = 0.0) qcp.matrices.matrix.Matrix[source]

Get the actual Unitary Matrix the enum corresponds to

Parameters

val (float) – Optional value required when creating certain matrix types

Returns

The Unitary Matrix

Return type

Matrix

classmethod list()[source]

Return all the enum options’ values

Returns

All the strings the enums correspond to

Return type

List[str]

qcp.algorithms.sudoku module

class qcp.algorithms.sudoku.Sudoku[source]

Bases: qcp.algorithms.abstract_algorithm.GeneralAlgorithm

__init__()[source]
_abc_impl = <_abc._abc_data object>
construct_circuit()[source]

Constructs the circuit to solve the sudoku problem by implementing the hadamard on the first 4 qubits, then the oracle gate across the entire circuit, followed by the diffusion gate that acts on the first 4 qubits applying the oracle and diffuser twice to maximise the amplitude of the solution

Returns

Matrix representing our completed Grover’s algorithm for sudoku

diffusion()[source]

Creates a diffusion gate - a gate which amplifies the probability of selecting our target state

Returns

Matrix representing diffusion gate

Return type

Matrix

measure_solution() Tuple[List[str], float][source]

Randomly measures 1 of 16 possible options that the 4 input variables can be with the probability of measuring a certain option determined by the probabilities associated with each state :return:

Tuple[List[str], float]: The input solution observed and the probability of observing that solution

oracle()[source]

The oracle gate for this problem checks the inputs to see whether the conditions have been met, using self.sudoku_conditions(), stores whether all of the conditions have been met or not in the 9th qubit by using a cnot gate, and then resets all the condition qubits that were changed by self.sudoku_conditions()

Returns

Matrix: representing the oracle gate

sudoku_conditions()[source]

For the 2x2 sudoku board there are 4 conditions which must be true for a solution to be valid: V0 != V1 V0 != V2 V1 != V3 V2 != V3 the variables cond1,..,cond4 respectively enforce these conditions by applying XOR gates across the relevant input qubits and outputting in the relative condition qubit :return: Matrix: representing all of the sudoku conditions

Module contents