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

[WIP] Add prototype circuit drawer #42

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion dwave/gate/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import itertools

from dwave.gate.drawer import CircuitDrawer

__all__ = [
"CircuitError",
"Circuit",
Expand Down Expand Up @@ -363,7 +365,7 @@ def add_qregister(self, num_qubits: int = 0, label: Hashable = None) -> None:
if label in self._qregisters:
raise ValueError(f"Quantum register {label} already present in the circuit.")

data = (Qubit(str(i)) for i in range(self.num_qubits, self.num_qubits + num_qubits))
data = (Qubit(i) for i in range(self.num_qubits, self.num_qubits + num_qubits))
self._qregisters[label] = QuantumRegister(data)

# remove cached 'qubits' attribute when updating 'qregisters'
Expand Down Expand Up @@ -509,6 +511,10 @@ def find_bit(self, bit: Bit, creg_label: bool = False) -> Tuple[Hashable, int]:

raise ValueError(f"Bit {bit} not found in any register.")

def draw(self):
"""Draw the circuit in the console using the built-in Circuit Drawer."""
return CircuitDrawer.draw_circuit(circuit=self)

def to_qasm(self, version: str = "2.0", **kwargs) -> str:
"""Converts the Circuit into an OpenQASM string.

Expand Down
18 changes: 18 additions & 0 deletions dwave/gate/drawer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 D-Wave Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Circuit drawer which uses unicode characters used to draw circuits in the terminal."""

from dwave.gate.drawer.column import *
from dwave.gate.drawer.drawer import *
65 changes: 65 additions & 0 deletions dwave/gate/drawer/characters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2023 D-Wave Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unicode characters used to draw circuits in the terminal."""

__all__ = [
"lang",
"rang",
"vbar",
"hbar",
"crs",
"tle",
"tri",
"ble",
"bri",
"lba",
"rba",
"tco",
"bco",
"cpl",
"blk",
"tbk",
"bbk",
"swp",
"spc",
]

lang = "\u27E8" # ⟨
# rang = u'\u2771' # ⟩
# rang = "\u276F" # ⟩
rang = "\u27E9" # ⟩
# vbar = u'\u007C' # │
vbar = "\u2502" # │
hbar = "\u2500" # ─

crs = "\u253C" # ┼
tle = "\u250C" # ┌
tri = "\u2510" # ┐
ble = "\u2514" # └
bri = "\u2518" # ┘

lba = "\u2524" # ┤
rba = "\u251C" # ├
tco = "\u2534" # ┴
bco = "\u252C" # ┬

cpl = "\u2295" # ⊕
blk = "\u2588" # █
tbk = "\u2584" # ▄
bbk = "\u2580" # ▀

swp = "\u2573" # ╳
# spc = u'\u2007' # space
spc = "\u0020" # space
Loading