Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ __pycache__/
# C extensions
*.so

# virtual environment
*/docsenv/

# Distribution / packaging
.Python
build/
docsenv/Lib/site-packages/pip/_internal/operations/build/
develop-eggs/
dist/
downloads/
Expand Down
72 changes: 70 additions & 2 deletions quantumcat/algorithms/grovers_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,26 @@


class GroversAlgorithm:
"""docstring for Circuit."""

"""
The GroversAlgorithm class functions to enable all the requisites for the implemetation of Grover's Algorithm
for the cases in which the solutions are known and unknown.
"""
def __init__(self, clause_list=[], input_arr=[], search_keyword=None, solution_known='N', flip_output=False, num_of_iterations=None):
"""
Initializes the GroversAlgorithm class and enable the smooth running of all the included
operations.

Args:
clause_list: clause list.
input_arr: input array.
search_keyword: keyword for searching database.
solution_known: if solution is known.
flip_output: output flip.
num_of_iterations: number of iterations required.

Raises:
CircuitError: If arguements do not correspond to qubit index.
"""
super(GroversAlgorithm, self).__init__()
self.clause_list = clause_list
self.input_arr = input_arr
Expand All @@ -34,6 +51,15 @@ def __init__(self, clause_list=[], input_arr=[], search_keyword=None, solution_k
self.num_of_iterations = self.get_num_of_iterations()

def initialize(self, provider):
"""
Initialize function carries out the task of initializing the algorithm to solve the problem under consideration.

Args:
provider: backend provider name.

Raises:
CircuitError: If arguments do not match.
"""
self.total_qubits = (2 * self.num_of_qubits) + 1 if self.solution_known == 'N' else (self.num_of_qubits + 1)
self.circuit = QCircuit(self.total_qubits, self.num_of_qubits)
self.provider = provider
Expand All @@ -51,6 +77,10 @@ def initialize(self, provider):
self.circuit.h_gate(self.total_qubits - 1)

def oracle_for_unknown_solution(self):
"""
Creates the oracle for finding the solution, when the solution to the problem is not known
beforehand.
"""
output_qubit = self.total_qubits - 1
clause_qubits = []

Expand All @@ -75,6 +105,9 @@ def oracle_for_unknown_solution(self):
self.XOR(self.clause_list[index], clause_qubit)

def oracle_for_known_solution(self):
"""
Creates the oracle for the problem under consideration, when the solution is known beforehand.
"""
output_qubit = self.total_qubits - 1
input_qubits = []
keyword = self.search_keyword
Expand All @@ -90,10 +123,23 @@ def oracle_for_known_solution(self):
self.circuit.x_gate(index)

def XOR(self, qubits, output_qubit):
"""
Generates and XOR classical logic gate.

Args:
qubits: list of qubit indices as input for XOR gate.
output_qubit: index of target qubit.

Raises:
CircuitError: If arguments do not match.
"""
for qubit in qubits:
self.circuit.cx_gate(qubit, output_qubit)

def diffuser(self):
"""
Creates the diffuser circuit for running the algorithm.
"""
q_circuit = self.circuit
qubits = self.num_of_qubits

Expand All @@ -114,6 +160,16 @@ def diffuser(self):
q_circuit.h_gate(qubit)

def execute(self, provider=providers.DEFAULT_PROVIDER, repetitions=10):
"""
Executes the circuit by calling in the provider that executes the algorithm.

Args:
provider: backend provider as a string input.
repetitions: number of executions of the circuit.

Returns:
circuit: solution after executing the algorithm.
"""
self.initialize(provider)

for _ in range(self.num_of_iterations):
Expand All @@ -129,7 +185,19 @@ def execute(self, provider=providers.DEFAULT_PROVIDER, repetitions=10):
return self.circuit.execute(provider=provider, repetitions=repetitions)

def draw_grovers_circuit(self):
"""
Generates circuit diagram of the Grover's algorithm built for the problem.

Returns:
figure: schematic representation of circuit for the algorithm.
"""
return self.circuit.draw_circuit(provider=self.provider)

def get_num_of_iterations(self):
"""
Calculates the number of iterations of the algorithm required for a reliable result.

Returns:
int: integral value of the number of iterations required.
"""
return int(0.5*(0.5*np.pi/np.arcsin(1/(np.sqrt(2 ** self.num_of_qubits)))-1))
31 changes: 30 additions & 1 deletion quantumcat/applications/generator/random_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@


class RandomNumber:

"""
RandomNumber class provides the requisite functions and definitions for creating a true random number generator circuit.
These numbers are created using intrinsically random quantum physical principles.
"""
def __init__(self, length, output_type=constants.BINARY):
"""
Initializes the RandomNumber class and enable the smooth running of all the included
operations.

Args:
length: length of random number to be generated.
output_type: the form of representation of the random number.
"""
super(RandomNumber, self).__init__()
self.length = length
self.output_type = output_type
Expand All @@ -28,6 +39,18 @@ def __init__(self, length, output_type=constants.BINARY):

def execute(self, provider=providers.DEFAULT_PROVIDER,
simulator_name=constants.DEFAULT_SIMULATOR, api=None, device=None):
"""
Executes the circuit by calling in the provider that executes the algorithm.

Args:
provider: backend provider as a string input
simulator_name: number of executions of the circuit
api: API token as string input, of backend to be executed.
device: name of specific quantum device to be used for execution.

Returns:
random_number: random number generated.
"""
num_qubits = self.length
circuit = QCircuit(num_qubits)
self.circuit = circuit
Expand All @@ -49,6 +72,12 @@ def execute(self, provider=providers.DEFAULT_PROVIDER,
return random_number

def draw_random_number_circuit(self):
"""
Generates circuit diagram of the random number generator algorithm.

Returns:
figure: schematic representation of circuit for the algorithm.
"""
return self.circuit.draw_circuit(provider=self.provider)


Loading