Skip to content

Commit

Permalink
chore:linter
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasM937 committed Jan 31, 2024
1 parent 019b17a commit 7dddebc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 54 deletions.
121 changes: 76 additions & 45 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''The Main file is intendet to be run ones.
"""The Main file is intendet to be run ones.
It runs the self checks as well as the sorting and order of particapents
usage:
./main.py [options]'''
./main.py [options]"""

import os
import json
Expand All @@ -18,66 +18,95 @@


def create_order(lst, participants):
'''This method is intendet to append the participants to a given list.'''
"""This method is intendet to append the participants to a given list."""
if participants not in lst:
lst.append(participants)
return lst


class Participants:
'''A Participant is an entity that participate at the secret santa event.
"""A Participant is an entity that participate at the secret santa event.
All Participants need a name and an id, while the reciver is left blank at first.
'''
"""

def __init__(self, id, name, receiver):
self.id = id
self.name = name
self.receiver = receiver # person_id to receive

if __name__ == '__main__':

if __name__ == "__main__":
parser = argparse.ArgumentParser()

parser.add_argument('-t', '--test', action='store_true',
help='The run is only for testing purposes.')
parser.add_argument('-vo','--validate_off', action='store_true',
help='Disable the validation. I advise against it but I\'m not your mum')
parser.add_argument('-ll','--loop_lebel', choices=range(1,5), default=2,
help='Specifies the min alowed length of loops')
parser.add_argument('-p', '-e', '--participants', '--elfs',
help='A way to add participants to the script.')
parser.add_argument('-cp', '--config_path', type=pathlib.Path,
default=os.getcwd()+'/config.json',
help='Specify the relative parth form point of execution.')
parser.add_argument('-b','--backend'),
parser.add_argument('-k','--provider_key')
parser.add_argument('-v', '--verbose',
action='store_false') # on/off flag
parser.add_argument(
"-t",
"--test",
action="store_true",
help="The run is only for testing purposes.",
)
parser.add_argument(
"-vo",
"--validate_off",
action="store_true",
help="Disable the validation. I advise against it but I'm not your mum",
)
parser.add_argument(
"-ll",
"--loop_lebel",
choices=range(1, 5),
default=2,
help="Specifies the min alowed length of loops",
)
parser.add_argument(
"-p",
"-e",
"--participants",
"--elfs",
help="A way to add participants to the script.",
)
parser.add_argument(
"-cp",
"--config_path",
type=pathlib.Path,
default=os.getcwd() + "/config.json",
help="Specify the relative parth form point of execution.",
)
(parser.add_argument("-b", "--backend"),)
parser.add_argument("-k", "--provider_key")
parser.add_argument("-v", "--verbose", action="store_false") # on/off flag
args = parser.parse_args()

if not args.backend:
args.test = True

if args.test:
backend = 'ibmq_qasm_simulator'
backend = "ibmq_qasm_simulator"

print(args.test, args.backend)

try:
config_file = open(args.config_path)
except RuntimeError:
print('nothing here yet') #TODO: Add method to ask for config entries.
print("nothing here yet") # TODO: Add method to ask for config entries.

config_data = json.load(config_file)

if config_data['Provider_Key'] != '' and config_data['Provider_Key'] != '<Provider_Key>':
provider_token = config_data['Provider_Key']
if (
config_data["Provider_Key"] != ""
and config_data["Provider_Key"] != "<Provider_Key>"
):
provider_token = config_data["Provider_Key"]
else:
if args.provider_key is not None:
provider_token = args.provider_key
else:
raise ValueError('No provider_token, configure config.json or use the CLI argument.')
raise ValueError(
"No provider_token, configure config.json or use the CLI argument."
)

provider = IBMProvider(token=provider_token)

num_participants = len(config_data['Participants'])
num_participants = len(config_data["Participants"])
num_qbits = math.ceil(math.log2(num_participants**2))

qc = QuantumCircuit(num_qbits, num_qbits)
Expand All @@ -86,36 +115,36 @@ def __init__(self, id, name, receiver):

qc.measure(list(range(num_qbits)), list(range(num_qbits)))

#TODO: Add CLI cmd to change this - simulator == -t / --test
backend = provider.get_backend('ibmq_qasm_simulator')
#TODO: Add CLI cmd to change this - also to split it up, to make retries less costly.
# TODO: Add CLI cmd to change this - simulator == -t / --test
backend = provider.get_backend("ibmq_qasm_simulator")
# TODO: Add CLI cmd to change this - also to split it up, to make retries less costly.
shots = 512

transpiled_circuit = transpile(qc, backend)

# Run the circuit and get result
print(f'start job on: {backend}')
print(f"start job on: {backend}")
job = backend.run(transpiled_circuit, shots=shots, memory=True)
result = job.result()
memory = result.get_memory()
print(f'job finished on {backend}')
print(f"job finished on {backend}")

part_list = []
for i in config_data['Participants']:
part_list.append(Participants(i, i['name'], None))
for i in config_data["Participants"]:
part_list.append(Participants(i, i["name"], None))

outputArray = []
# convert the result to decimal
for x in range(0, shots):
converted = int(memory[x], 2)
outputArray.append(converted%len(part_list))
outputArray.append(converted % len(part_list))

order = []
i = 0
while len(order) < len(part_list):
create_order(order, outputArray[0])
outputArray.pop(0)
i +=1
i += 1

orderd_list = [part_list[i] for i in order]

Expand All @@ -125,22 +154,24 @@ def __init__(self, id, name, receiver):
if test_other_num_still_pressent(orderd_list[i].id, part_list, outputArray):
while outputArray[j] == orderd_list[i].id:
j += 1
j = j%len(outputArray)
j = j % len(outputArray)
temp = outputArray[j]
orderd_list[i].receiver = part_list[temp]
outputArray = list(filter((temp).__ne__, outputArray))
else:
raise Exception('''We encounted an issue during generation, restart script.
- may the randomness be with you''')
raise Exception(
"""We encounted an issue during generation, restart script.
- may the randomness be with you"""
)

# Tests:
print(f'all Participants have some one: {test_all_give_someone(part_list)}')
print(f"all Participants have some one: {test_all_give_someone(part_list)}")

print(f'no self loops: {test_no_self_loops(part_list)}')
print(f"no self loops: {test_no_self_loops(part_list)}")

print(f'no second order loops: {test_no_second_order_loops(part_list)}')
print(f"no second order loops: {test_no_second_order_loops(part_list)}")

# Output:
print('the result is:')
print("the result is:")
for p in part_list:
print(f'{p.name.ljust(15)}, {p.receiver.name}')
print(f"{p.name.ljust(15)}, {p.receiver.name}")
23 changes: 14 additions & 9 deletions src/test.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
'''This file is used to store and menage the tests for the script'''
"""This file is used to store and menage the tests for the script"""


def test_all_give_someone(part_list):
'''Test if all particepants sends to someone'''
"""Test if all particepants sends to someone"""
for p in part_list:
if p.receiver is None:
return False
return True

#TODO: Test if all particepants recieve from someone.

# TODO: Test if all particepants recieve from someone.


def test_no_self_loops(part_list):
'''Test if self loops are present.
Self loops meen that one particepant draw him/her/them -self'''
"""Test if self loops are present.
Self loops meen that one particepant draw him/her/them -self"""
for p in part_list:
if p.id == p.receiver.id:
return False
return True


def test_no_second_order_loops(part_list):
'''Test if the cardinality of a smallest loop is larger than two
- optional criteria'''
"""Test if the cardinality of a smallest loop is larger than two
- optional criteria"""
for p in part_list:
if p.receiver is not None:
if p.receiver.receiver is not None:
if p.id == p.receiver.receiver.id:
return False
return True


def test_other_num_still_pressent(number, part_list, output_array):
'''Test if the remainding particepants IDs are still preasent in the randome array.
This is an indication for a deadlock.'''
"""Test if the remainding particepants IDs are still preasent in the randome array.
This is an indication for a deadlock."""
for i in range(len(part_list)):
if (i is not number) and (i in output_array):
return True
Expand Down

0 comments on commit 7dddebc

Please sign in to comment.