Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up main.py, in preparation for integrating user-facing features #67

Merged
merged 7 commits into from
Jun 29, 2024
80 changes: 24 additions & 56 deletions python/anyon_braiding_simulator/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Standard Library
import cmd
import subprocess
import sys

from anyon_braiding_simulator.anyon_braiding_simulator import (
Expand Down Expand Up @@ -57,8 +56,7 @@ def anyon(*args):
# Make sure any previous anyons were specified in 1D space (i.e. without a position argument)
if anyons and sim.get_dim_of_anyon_pos() == 2:
print(
'Error: you have already provided an anyon in 2D space, so the rest must also have a \
specified 2D position'
'\nError: you have already provided an anyon in 2D space, so the rest must also have a specified 2D position'
)
return
elif not anyons:
Expand All @@ -70,14 +68,14 @@ def anyon(*args):
# Make sure any previous anyons were specified in 2D space
if sim.get_dim_of_anyon_pos() == 1:
print(
'Error: you have already provided an anyon in 1D space, so the positions of the rest \
cannot be specified in 2D'
'\nError: you have already provided an anyon in 1D space, so the positions of the rest cannot be specified in 2D'
)
return

try:
position = tuple(map(float, args[2].replace('{', '').replace('}', '').split(',')))
position[1]
if len(position) != 2:
raise ValueError
except ValueError:
print('Error: position must be formatted as {x,y} where x and y are numbers')
return
Expand All @@ -89,11 +87,11 @@ def anyon(*args):
try:
sim.update_anyons(True, [new_anyon])
if len(args) == 2:
print(f'\nCreated anyon {name} with TC {topological_charge} at position {position[0]} in 1D')
print(f'\nCreated anyon {name} with TC {topological_charge} at position {position[0]} in 1D.')
else:
print(f'\nCreated anyon {name} with TC {topological_charge} at position {position} in 2D')
print(f'\nCreated anyon {name} with TC {topological_charge} at position {position} in 2D.')
except ValueError:
print('\nError: An anyon with the same name already exists')
print('Error: An anyon with the same name already exists')


def model(*args):
Expand All @@ -115,31 +113,6 @@ def model(*args):
model = Model(model_convert[model_type.lower()])
sim.set_model(model)


def fusion(*args):
"""
Handle the fusion command. This command executes the various fusion operations.
"""
if len(args) < 1:
print('Error: Not enough arguments')
return

fusion = sim._fusion
cmd = args[0]

if cmd.lower() == 'fuse':
# anyon_pairs = [tuple(anyon.replace('-', ' ').split()) for anyon in args[1:]]
# anyon_indices = sim.pairs_to_indices(anyon_pairs)
# fusion.fuse(anyon_indices)
pass

elif cmd.lower() == 'print':
# print(fusion)
pass
else:
print('Error: Unknown fusion command')


def braid(*args):
"""
Handle the braid command. This command executes the various braid operations.
Expand Down Expand Up @@ -178,12 +151,11 @@ class SimulatorShell(cmd.Cmd):

def __init__(self):
super().__init__()
self.prompt = 'simulator> '
self.prompt = '\nsimulator> '

self.command_options = {
'anyon': 'anyon <name> <topological charge> <{x,y} coords>',
'model': 'model <Ising or Fibonacci>',
'fusion': 'fusion anyon_name_1-anyon_name_2 ...',
'braid': 'braid anyon_name_1-anyon_name_2 ...',
'list': 'list',
}
Expand All @@ -209,7 +181,7 @@ def __init__(self):
if no_anyons:
user_input = input(
'\nEnter the anyon name, topological charge, and optionally, the 2D position.'
'\nUse the format <name> <topological charge> <{x,y}>.\n'
'\nUse the format "<name> <topological charge> <{x,y}>".\n'
'> '
)
else:
Expand All @@ -225,7 +197,19 @@ def __init__(self):
print('\nError: At least 3 anyons are required to initialize the simulation.')
continue

args = user_input.split(' ')
# Check for 2D position in input such that space is allowed (ex. {4, 5})
if '{' in user_input and '}' in user_input:
start = user_input.find('{')
end = user_input.find('}') + 1
coords = user_input[start:end]

mod_input = user_input.replace(coords, "COORDS_PLACEHOLDER")
args = mod_input.split()
# Replace placeholder with original coords (spaces removed)
args[args.index("COORDS_PLACEHOLDER")] = coords.replace(" ", "")
else:
args = user_input.split(' ')

if len(args) < 2 or len(args) > 3:
print('Error: There should be either 2 or 3 arguments')
continue
Expand All @@ -235,14 +219,6 @@ def __init__(self):

self.init_complete = True

def do_shell(self, arg):
"Run a shell command"
print('running shell command:', arg)
subprocess.run(
arg,
shell=True,
)

def do_anyon(self, arg):
"Add an anyon to the simulation"
if self.init_complete:
Expand All @@ -268,14 +244,6 @@ def do_model(self, arg):
else:
model(*args)

def do_fusion(self, arg):
"Fuse anyons together"
args = arg.split(' ')
if args[0] == 'help' or args[0] == '-h':
print(self.command_options['fusion'])
else:
fusion(*args)

def do_braid(self, arg):
"Braid anyons together"
args = arg.split(' ')
Expand All @@ -299,8 +267,8 @@ def do_exit(self, arg):

def do_help(self, arg):
"Print help"
cmds = ['anyon', 'model', 'fusion', 'braid', 'exit', 'list']
print(f'Commands: {", ".join(sorted(cmds))}')
cmds = ['braid', 'exit', 'list']
print(f'\nCommands: {", ".join(sorted(cmds))}')


if __name__ == '__main__':
Expand Down
Loading