Skip to content

Commit

Permalink
added classical_walk, removed abstract class for now
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasvinaya committed Sep 29, 2024
1 parent e978990 commit 6f9645e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pre-commit.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ echo "Formatting $file with yapf"
python -m yapf -i "$file"

# Type check qtsit package
echo "Type checking deepchem package with mypy"
echo "Type checking qtsit package with mypy"
python -m mypy -p qtsit

# Lint file with flake8 and show count
Expand Down
43 changes: 22 additions & 21 deletions qtsit/algorithms/randomwalk.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
"""
Code for random walk algorithms.
"""

import random
import qiskit
import numpy as np


# abstract class walk - will be used in classical walk and quantum walk
class walk:
"""Abstract class for random walk algorithms.
class classical_walk:
"""Classical random walk algorithm.
"""

def __init__(self,
Expand All @@ -18,8 +14,8 @@ def __init__(self,
steps,
j,
k,
groundtruth,
toss_val=0.1) -> None:
ground_truth,
toss_val: float = 0.1) -> None:
self.size = size
self.j = j
self.k = k
Expand All @@ -30,20 +26,24 @@ def __init__(self,
for _ in range(size):
self.walking_space.append([0.5, 0, 0.5])

self.pos = []
self.pos: list[float] = []
self.pos_index = initial_pos
self.ground_truth = groundtruth
self.ground_truth = ground_truth
self.emp_truth = []
for i in range(size):
for _ in range(size):
self.emp_truth.append(0.0)
self.happiness = [0 for i in range(self.size)]
self.hits = [0 for i in range(self.size)]
self.cumsum = 0

def goto_pos(self):
def goto_pos(self) -> None:
"""Go to the position.
"""
self.pos = self.walking_space[self.pos_index]

def coin_toss(self):
def coin_toss(self) -> None:
"""Coin toss function.
"""
self.toss_val = np.random.rand()

if (0 <= self.toss_val <= self.pos[0]):
Expand All @@ -53,7 +53,9 @@ def coin_toss(self):
if (self.pos[0] + self.pos[1] < self.toss_val <= 1):
self.result = "FORWARD"

def one_walk(self):
def one_walk(self) -> None:
"""One walk function.
"""
if (self.pos_index == self.size - 1):
if (self.result == "BACK"):
self.pos_index = self.pos_index - 1
Expand Down Expand Up @@ -104,16 +106,15 @@ def explore(self):
self.walking_space[j][2] = self.walking_space[j][0]
self.pos_index = np.array(self.emp_truth).argmax()

def main(self):
"""Main function.
"""
self.explore()

class classical_walk(walk):
"""Classical random walk algorithm.
"""

def __init__(self, graph):
super().__init__(graph)
return sum(self.happiness)


class quantum_walk(walk):
class quantum_walk():
"""Quantum random walk algorithm.
"""

Expand Down

0 comments on commit 6f9645e

Please sign in to comment.