Skip to content

Commit

Permalink
Make circuit input more general to allow other formats in the future.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlyongemallo committed Dec 22, 2023
1 parent 35372cb commit 5f830a2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
4 changes: 2 additions & 2 deletions zxlive/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ToolType(IntEnum):
"path/custom-rules": "lemmas/",
"color-scheme": "modern-red-green",
"snap-granularity": '4',
"qasm-flavor": 'openqasm',
"input-circuit-format": 'openqasm',

"tikz/boundary-export": pyzx.settings.tikz_classes['boundary'],
"tikz/Z-spider-export": pyzx.settings.tikz_classes['Z'],
Expand Down Expand Up @@ -83,7 +83,7 @@ class ToolType(IntEnum):
'gidney': "Gidney's Black & White",
}

qasm_flavor = {
input_circuit_formats = {
'openqasm': "standard OpenQASM",
'sqasm': "Spider QASM",
'sqasm-no-simplification': "Spider QASM (no simplification)",
Expand Down
12 changes: 3 additions & 9 deletions zxlive/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,9 @@ def import_diagram_dialog(parent: QWidget) -> Optional[ImportGraphOutput | Impor
return import_diagram_from_file(file_path, selected_filter)


def create_circuit_dialog(parent: QWidget) -> Optional[str]:
"""Shows a dialog to input a circuit in QASM format."""
explanation = """Write a circuit in QASM format. Example:
qreg q[3];
cx q[0], q[1];
h q[2];
ccx q[0], q[1], q[2];
"""
s, success = QInputDialog.getMultiLineText(parent, "Circuit input", explanation, "qreg q[3];\n")
def create_circuit_dialog(explanation: str, example: str, parent: QWidget) -> Optional[str]:
"""Shows a dialog to input a circuit."""
s, success = QInputDialog.getMultiLineText(parent, "Circuit input", explanation, example)
return s if success else None


Expand Down
23 changes: 16 additions & 7 deletions zxlive/edit_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from .base_panel import ToolbarSection
from .commands import UpdateGraph
from .common import GraphT
from .common import GraphT, input_circuit_formats
from .dialogs import show_error_msg, create_circuit_dialog
from .editor_base_panel import EditorBasePanel
from .graphscene import EditGraphScene
Expand Down Expand Up @@ -74,26 +74,35 @@ def _start_derivation(self) -> None:

def _input_circuit(self) -> None:
settings = QSettings("zxlive", "zxlive")
flavor = settings.value("qasm-flavor")
qasm = create_circuit_dialog(self)
circuit_format = str(settings.value("input-circuit-format"))
explanations = {
'openqasm': "Write a circuit in QASM format.",
'sqasm': "Write a circuit in Spider QASM format.",
'sqasm-no-simplification': "Write a circuit in Spider QASM format. No simplification will be performed.",
}
examples = {
'openqasm': "qreg q[3];\ncx q[0], q[1];\nh q[2];\nccx q[0], q[1], q[2];",
'sqasm': "qreg q[1];\nqreg A[2];\n;s A[1];\n;cx q[0], A[0];\n;cx q[0], A[1];",
'sqasm-no-simplification': "qreg q[1];\nqreg Z[2];\ncx q[0], Z[0];\ncx q[0], Z[1];\ns Z[1];",
}
qasm = create_circuit_dialog(explanations[circuit_format], examples[circuit_format], self)
if qasm is not None:
new_g = copy.deepcopy(self.graph_scene.g)
try:
if flavor == 'sqasm':
if circuit_format == 'sqasm':
circ = sqasm(qasm)
elif flavor == 'sqasm-no-simplification':
elif circuit_format == 'sqasm-no-simplification':
circ = sqasm(qasm, simplify=False)
else:
circ = QASMParser().parse(qasm, strict=False).to_graph()
except TypeError as err:
show_error_msg("Invalid circuit", str(err))
return
except Exception:
show_error_msg("Invalid circuit", "Couldn't parse QASM code")
show_error_msg("Invalid circuit", f"Couldn't parse code as {input_circuit_formats[circuit_format]}.")
return

new_verts, new_edges = new_g.merge(circ)
cmd = UpdateGraph(self.graph_view, new_g)
self.undo_stack.push(cmd)
self.graph_scene.select_vertices(new_verts)

4 changes: 2 additions & 2 deletions zxlive/settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import pyzx

from .common import set_pyzx_tikz_settings, colors, setting, color_schemes, qasm_flavor, defaults
from .common import set_pyzx_tikz_settings, colors, setting, color_schemes, input_circuit_formats, defaults

if TYPE_CHECKING:
from .mainwindow import MainWindow
Expand Down Expand Up @@ -63,7 +63,7 @@ def __init__(self, main_window: MainWindow) -> None:
self.add_setting(form_general, "color-scheme", "Color scheme", 'combo',data=color_schemes)
self.add_setting(form_general, "snap-granularity", "Snap-to-grid granularity", 'combo',
data = {'2': "2", '4': "4", '8': "8", '16': "16"})
self.add_setting(form_general, "qasm-flavor", "Input Circuit as", 'combo', data=qasm_flavor)
self.add_setting(form_general, "input-circuit-format", "Input Circuit as", 'combo', data=input_circuit_formats)
self.prev_color_scheme = self.settings.value("color-scheme")
vlayout.addStretch()

Expand Down

0 comments on commit 5f830a2

Please sign in to comment.