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

fixed print str #32

Merged
merged 1 commit into from
Jun 9, 2024
Merged
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
59 changes: 35 additions & 24 deletions src/Braiding.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def swap(self, anyon_A, anyon_B):
# Perform the swap if both indices are valid and the anyons are next to each other
if index_A is not None and index_B is not None:
self.anyons[index_A], self.anyons[index_B] = self.anyons[index_B], self.anyons[index_A]
self.operations.append((index_A, index_B)) # Update the operations list
else:
print('The specified anyons could not be swapped')

Expand All @@ -97,33 +98,43 @@ def __str__(self) -> str:
if not self.operations:
print('No operations to print')
return ''

# Initialize the output for each anyon
num_anyons = len(self.anyons)
output = [['|' for _ in range(num_anyons)] for _ in range(len(self.operations) * 5)]
max_rows = len(self.operations) * 5 # Each swap occupies 5 rows
output = [[' ' for _ in range(num_anyons * 5)] for _ in range(max_rows)]

spacing = 4 # 3 spaces between cols

# Add '|' for non-swap columns
for col in range(num_anyons):
for step, (index_A, index_B) in enumerate(self.operations):
base = step * 5
if col != index_A and col != index_B:
for i in range(5):
output[base + i][col * spacing + 4] = '|'

# Apply each recorded operation to the output
for step, (index_A, index_B) in enumerate(self.operations):
base = step * 5
base = step * 5 # Base for each swap operation
if index_A < index_B:
output[base + 0][index_A] = '\\'
output[base + 1][index_A + 1] = '\\'
output[base + 2][index_A + 1] = '\\'
output[base + 3][index_A + 1] = '/'
output[base + 4][index_A] = '/'
output[base + 4][index_B] = '/'
output[base + 3][index_B - 1] = '/'
output[base + 2][index_B - 1] = '/'
output[base + 1][index_B - 1] = '\\'
for i in range(3):
output[base + i][index_A * spacing + 4 + i * 1] = '\\'
output[base + i][index_B * spacing + 4 - i * 1] = '/'
for i in range(3, 5):
output[base + i][index_A * spacing + 4 + (5 - i - 1) * 1] = '/'
output[base + i][index_B * spacing + 4 - (5 - i - 1) * 1] = '\\'

else:
output[base + 0][index_B] = '\\'
output[base + 1][index_B + 1] = '\\'
output[base + 2][index_B + 1] = '\\'
output[base + 3][index_B + 1] = '/'
output[base + 4][index_B] = '/'
output[base + 4][index_A] = '/'
output[base + 3][index_A - 1] = '/'
output[base + 2][index_A - 1] = '/'
output[base + 1][index_A - 1] = '\\'

return '\n'.join([' '.join(row) for row in output])
for i in range(3):
output[base + i][index_B * spacing + 4 + i * 1] = '\\'
output[base + i][index_A * spacing + 4 - i * 1] = '/'
for i in range(3, 5):
output[base + i][index_B * spacing + 4 + (5 - i - 1) * 1] = '/'
output[base + i][index_A * spacing + 4 - (5 - i - 1) * 1] = '\\'

return '\n'.join([''.join(row) for row in output if any(c != ' ' for c in row)])

# Function to test __str__ after each timestep
def print_anyons_state(braid, description):
anyon_names = [anyon.name for anyon in braid.anyons]
print(f"{description}: [{', '.join(anyon_names)}]")
Loading