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

Add circuit input dialog #182

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion zxlive/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from PySide6.QtCore import QFile, QIODevice, QTextStream
from PySide6.QtWidgets import (QDialog, QDialogButtonBox, QFileDialog,
QFormLayout, QLineEdit, QMessageBox,
QPushButton, QTextEdit, QWidget)
QPushButton, QTextEdit, QWidget, QInputDialog)
from pyzx import Circuit, extract_circuit

from .common import GraphT
Expand Down Expand Up @@ -102,6 +102,11 @@ 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]:
s, success = QInputDialog.getMultiLineText(parent, "Circuit input", "Write a circuit in QASM format", "qreg qs[3];\n")
return s if success else None


def import_diagram_from_file(file_path: str, selected_filter: str = FileFormat.All.filter) -> \
Optional[ImportGraphOutput | ImportProofOutput | ImportRuleOutput]:
"""Imports a diagram from a given file path.
Expand Down
31 changes: 29 additions & 2 deletions zxlive/edit_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from PySide6.QtCore import Signal
from PySide6.QtGui import QAction
from PySide6.QtWidgets import (QToolButton)
from pyzx import EdgeType, VertexType
from pyzx import EdgeType, VertexType, Circuit
from pyzx.circuit.qasmparser import QASMParser

from .base_panel import ToolbarSection
from .commands import UpdateGraph
from .common import GraphT
from .dialogs import show_error_msg
from .dialogs import show_error_msg, create_circuit_dialog
from .editor_base_panel import EditorBasePanel
from .graphscene import EditGraphScene
from .graphview import GraphView
Expand Down Expand Up @@ -48,11 +50,17 @@ def __init__(self, graph: GraphT, *actions: QAction) -> None:
def _toolbar_sections(self) -> Iterator[ToolbarSection]:
yield from super()._toolbar_sections()

self.input_circuit = QToolButton(self)
self.input_circuit.setText("Input Circuit")
self.input_circuit.clicked.connect(self._input_circuit)
yield ToolbarSection(self.input_circuit)

self.start_derivation = QToolButton(self)
self.start_derivation.setText("Start Derivation")
self.start_derivation.clicked.connect(self._start_derivation)
yield ToolbarSection(self.start_derivation)


def _start_derivation(self) -> None:
if not self.graph_scene.g.is_well_formed():
show_error_msg("Graph is not well-formed")
Expand All @@ -63,3 +71,22 @@ def _start_derivation(self) -> None:
if isinstance(phase, Poly):
phase.freeze()
self.start_derivation_signal.emit(new_g)

def _input_circuit(self) -> None:
qasm = create_circuit_dialog(self)
if qasm is not None:
new_g = copy.deepcopy(self.graph_scene.g)
try:
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")
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)

Loading