Skip to content

Commit

Permalink
Fix mypy and arrange utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jond01 committed May 20, 2022
1 parent 53b41a1 commit 883f2b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion quantum-viz/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ show_error_codes = true
show_error_context = true

[[tool.mypy.overrides]]
module = ["IPython.core.display", "varname.*"]
module = ["IPython.*", "varname.*", "qiskit.*"]
ignore_missing_imports = true

[build-system]
Expand Down
7 changes: 4 additions & 3 deletions quantum-viz/quantum_viz/qiskit_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Iterator
from typing import List
from typing import Optional
from typing import Tuple
from typing import Type
from typing import Union

Expand Down Expand Up @@ -133,7 +134,7 @@ def __init__(
}
self.qubit2id: Dict[Qubit, int] = dict()
self._init_qubits()
self._clbit2id: Dict[Clbit, (int, int)] = dict()
self._clbit2id: Dict[Clbit, Tuple[int, int]] = dict()
self._update_qviz_dict()

def _init_qubits(self) -> None:
Expand Down Expand Up @@ -210,7 +211,7 @@ def _parse_operation(
depth: int,
) -> Dict:

op_dict = {"gate": self._get_instruction_name(instruction)}
op_dict: Dict = {"gate": self._get_instruction_name(instruction)}

if instruction.params:
self._add_params(op_dict, instruction)
Expand Down Expand Up @@ -348,7 +349,7 @@ def _update_condition(self, op_dict: Dict, instruction: Instruction) -> Dict:
if isinstance(classical, ClassicalRegister):
clbit = Clbit(classical, index=0)
else:
clbit: Clbit = classical
clbit = classical

if val:
render_condition = ConditionalRender.ON_ONE.value
Expand Down
19 changes: 13 additions & 6 deletions quantum-viz/quantum_viz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from typing import TYPE_CHECKING
from typing import Union

from widget import Viewer
from .widget import DEFAULT_STYLE
from .widget import Style

if TYPE_CHECKING:
from qiskit import QuantumCircuit
Expand Down Expand Up @@ -44,13 +45,18 @@
</html>
"""
SUFFIX = "_qviz.html"
STYLES = ("Default", "BlackAndWhite", "Inverted") # Use Literal type in python 3.8

STYLES = list(Style)


class UnsupportedStyleWarning(UserWarning):
"""A warning raised when an unsupported style is chosen."""


def display(
circuit: Union[Dict[str, Any], "QuantumCircuit"],
filename: Union[str, Path, None] = None,
style: str = "Default",
style: Style = DEFAULT_STYLE,
version: Optional[str] = None,
**kwargs,
) -> None:
Expand All @@ -65,7 +71,8 @@ def display(
if style not in STYLES:
warnings.warn(
f"The selected style '{style}' is not supported and will be ignored.\n"
f"The supported styles are {STYLES}"
f"The supported styles are {STYLES}",
UnsupportedStyleWarning,
)

if version is None:
Expand All @@ -74,9 +81,9 @@ def display(
version = "@" + version

if not isinstance(circuit, dict):
from quantum_viz.qiskit_parser import qiskit2dict
from .qiskit_parser import qiskit2dict

circuit = qiskit2dict(circuit)
circuit = qiskit2dict(circuit, **kwargs)

qviz_json = json.dumps(circuit)

Expand Down

0 comments on commit 883f2b5

Please sign in to comment.