Skip to content

Commit 26606fc

Browse files
committed
Linter changes
1 parent 0806501 commit 26606fc

13 files changed

Lines changed: 137 additions & 65 deletions

File tree

examples/distributed/teleport/teleport-alice.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from netqasm.runtime.settings import set_simulator
22
set_simulator("simulaqron")
33

4-
from simulaqron.settings import simulaqron_settings
5-
simulaqron_settings.network_config_file = "./simulaqron_settings.json"
4+
from netqasm.sdk.external import NetQASMConnection # noqa: E402
5+
from netqasm.sdk import Qubit, EPRSocket # noqa: E402
66

7-
from netqasm.sdk.external import NetQASMConnection
8-
from netqasm.sdk import Qubit, EPRSocket
97

108
def run_alice():
119
epr_socket = EPRSocket("Bob")
@@ -26,4 +24,3 @@ def run_alice():
2624
if __name__ == "__main__":
2725
results = run_alice()
2826
print(f"Alice measurements: m1={results[0]}, m2={results[1]}")
29-
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
from netqasm.runtime.settings import set_simulator
22
set_simulator("simulaqron")
33

4-
from simulaqron.settings import simulaqron_settings
5-
simulaqron_settings.network_config_file = "./simulaqron_settings.json"
6-
7-
from netqasm.sdk.external import NetQASMConnection
8-
from netqasm.sdk import EPRSocket
4+
from netqasm.sdk.external import NetQASMConnection # noqa: E402
5+
from netqasm.sdk import EPRSocket # noqa: E402
96

107

118
def run_bob():
@@ -19,4 +16,3 @@ def run_bob():
1916
if __name__ == "__main__":
2017
result = run_bob()
2118
print(f"Bob measurement: {result}")
22-

simulaqron/cli.py

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
from simulaqron.network import Network
1313
from simulaqron.settings import LOCAL_SIMULAQRON_SETTINGS, LOCAL_NETWORK_SETTINGS, HOME_NETWORK_SETTINGS
14-
from simulaqron.settings import simulaqron_settings, get_default_network_config_file, network_config
15-
from simulaqron.settings.network_config import NodeConfig, DEFAULT_SIMULAQRON_NETWORK_FILENAME
14+
from simulaqron.settings import simulaqron_settings, network_config
15+
from simulaqron.settings.network_config import (NodeConfig, DEFAULT_SIMULAQRON_NETWORK_FILENAME,
16+
get_default_network_config_file)
1617
from simulaqron.settings.simulaqron_config import SimBackend
1718

1819
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
@@ -137,7 +138,10 @@ def _create_local_networks_if_needed_and_load():
137138
_load_local_network_or_default()
138139

139140

140-
@click.group(context_settings=CONTEXT_SETTINGS, epilog="Run 'simulaqron COMMAND --help' for more information on a command.")
141+
@click.group(
142+
context_settings=CONTEXT_SETTINGS,
143+
epilog="Run 'simulaqron COMMAND --help' for more information on a command."
144+
)
141145
def cli_entry_point():
142146
"""Command line interface for interacting with SimulaQron."""
143147
pass
@@ -162,10 +166,10 @@ def version():
162166
@cli_entry_point.command()
163167
@click.option(
164168
"--network-config-file",
165-
help=f"Path to network config file. If not specified, uses "
166-
f"./{DEFAULT_SIMULAQRON_NETWORK_FILENAME} or ~/.simulaqron/{DEFAULT_SIMULAQRON_NETWORK_FILENAME}",
169+
help=f"Path to network config file. If not specified, uses ./{DEFAULT_SIMULAQRON_NETWORK_FILENAME} " # noqa: E131
170+
f"or ~/.simulaqron/{DEFAULT_SIMULAQRON_NETWORK_FILENAME}", # noqa: E131
167171
type=click.Path(exists=True, dir_okay=False, resolve_path=True, path_type=Path),
168-
default=LOCAL_NETWORK_SETTINGS
172+
default=get_default_network_config_file()
169173
)
170174
@click.option(
171175
"--simulaqron-config-file",
@@ -189,51 +193,67 @@ def version():
189193
)
190194
def start(name: str, nodes: str, simulaqron_config_file: Path, network_config_file: Path):
191195
"""Starts a network with the given parameters or from config files."""
192-
# if netconfig is None:
193-
# network_config_file = get_default_network_config_file()
194196
# Checks the simulaqron config
195197
if not _path_exists(simulaqron_config_file):
196-
raise click.BadParameter(f"The given simulaqron config file '{simulaqron_config_file}' does not exist or it is a folder.\n"
197-
"Please check the path given to the --simulaqron-config-file option.")
198+
raise click.BadOptionUsage(
199+
option_name="simulaqron-config-file",
200+
message=f"The given simulaqron config file '{simulaqron_config_file}' does not exist or it "
201+
"is a folder.\nPlease check the path given to the --simulaqron-config-file option." # noqa: E131
202+
)
198203
# Checks the network config
199204
if not _path_exists(network_config_file):
200-
raise click.BadParameter(f"The given network config file '{network_config_file}' does not exist or it is a folder.\n"
201-
"Please check the path given to the --network-config-file option.")
205+
raise click.BadOptionUsage(
206+
option_name="network-config-file",
207+
message=f"The given network config file '{network_config_file}' does not exist or it is a "
208+
"folder.\nPlease check the path given to the --network-config-file option." # noqa: E131
209+
)
202210
# Load SimulaQron and network configs
203211
simulaqron_settings.read_from_file(simulaqron_config_file)
204212
network_config.read_from_file(network_config_file)
205213
# Check that the network name exists in the network configuration
206-
if not name in network_config.networks:
207-
raise click.BadParameter(f"The network '{name}' was not found in the network configuration file '{network_config_file}'.\n"
208-
f"Please check the name you passed in the --name option and try again.")
214+
if name not in network_config.networks:
215+
raise click.BadOptionUsage(
216+
option_name="name",
217+
message=f"The network '{name}' was not found in the network configuration file " # noqa: E713
218+
f"'{network_config_file}'.\nPlease check the name you passed in the" # noqa: E131
219+
" --name option and try again." # noqa: E131
220+
)
209221
# Check that the nodes to start exist in the given network
210222
nodes = nodes.split(",")
211223
if len(nodes) <= 0:
212-
print("The list of nodes to start is empty. Please check the list given in the --nodes argument.")
213-
return
224+
raise click.BadOptionUsage(
225+
option_name="nodes",
226+
message="The list of nodes to start is empty. Please check the list given in "
227+
"the --nodes argument."
228+
)
214229
for node_to_start in nodes:
215-
if not node_to_start in network_config.networks[name]:
216-
raise click.BadParameter(f"The node '{node_to_start}' was not found in the network named '{name} 'specified in"
217-
f" the configuration file '{network_config_file}'.\nPlease check the list of names you "
218-
f"passed in the --nodes option and try again.")
230+
if node_to_start not in network_config.networks[name]:
231+
raise click.BadOptionUsage(
232+
option_name="nodes",
233+
message=f"The node '{node_to_start}' was not found in the network named " # noqa: E713
234+
f"'{name} 'specified in the configuration file '{network_config_file}'.\n" # noqa: E131
235+
"Please check the list of names you passed in the --nodes option and try again." # noqa: E131
236+
)
219237
# Check that there is no other network with the same name running
220238
pidfile = PID_FOLDER / f"simulaqron_network_{name}.pid"
221239
if pidfile.exists():
222-
logging.warning("Network with name %s is already running", name)
223-
logging.warning("The pidfile for this network is located at %s", pidfile)
224-
return
240+
raise click.BadOptionUsage(
241+
option_name="pidfile",
242+
message=f"Network with name {name} is already running.\nThe pidfile for "
243+
f"this network is located at {pidfile}" # noqa: E131
244+
)
225245

226246
# Let's start the simulaqron daemon. We will pass the config file so it will be available
227247
# in the child process and load the same config
228-
d = SimulaQronDaemon(pidfile=pidfile, name=name, nodes=nodes)
248+
d = SimulaQronDaemon(pidfile=pidfile, name=name, nodes=nodes, network_config_file=network_config_file)
229249
try:
230250
d.start()
231251
except SystemExit as e:
232252
if e.code == exit.PIDFILE_INACCESSIBLE or \
233253
e.code == exit.DAEMONIZE_FAILED:
234254
logging.debug(f"Failed to launch Simulaqron Daemon. "
235255
f"Exit code reported by daemons: {e.code}")
236-
print("Failed to launch SimulaQron Daemon. Aborted!")
256+
raise click.BadParameter("Failed to launch SimulaQron Daemon. Aborted!")
237257

238258

239259
###############
@@ -251,7 +271,7 @@ def stop(name: str):
251271
"""Stops a network."""
252272
assert name is not None
253273
pidfile = PID_FOLDER / f"simulaqron_network_{name}.pid"
254-
logging.debug(f"Trying to open PIDfile")
274+
logging.debug("Trying to open PIDfile")
255275
if not pidfile.exists():
256276
logging.warning("Network with name %s is not running", name)
257277
return
@@ -289,7 +309,7 @@ def reset(force: bool):
289309
simulaqron_settings.default_settings()
290310
simulaqron_settings.write_to_file(LOCAL_SIMULAQRON_SETTINGS)
291311
else:
292-
print("Aborting!")
312+
raise click.ClickException("Aborting!")
293313

294314

295315
###############
@@ -637,7 +657,7 @@ def get(network_name: str):
637657
try:
638658
nodes = network_config.get_node_names(network_name=network_name)
639659
except ValueError:
640-
print(f"No network {network_name}")
660+
raise click.BadParameter(f"No network {network_name}")
641661
else:
642662
print(("{} " * len(nodes))[:-1].format(*nodes))
643663

simulaqron/netqasm_backend/qnodeos.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import os
32
import sys
43

54
from typing import Optional, Dict, Callable, Generator, Any, List, Type
@@ -16,6 +15,7 @@
1615
ReturnQubitStateMessage)
1716
from simulaqron.settings import simulaqron_settings
1817

18+
1919
class SubroutineHandler(QNodeController):
2020
def __init__(self, factory: "NetQASMFactory", instr_log_dir: Optional[str] = None, # noqa: F821
2121
flavour: Optional[Flavour] = None):
@@ -30,7 +30,7 @@ def __init__(self, factory: "NetQASMFactory", instr_log_dir: Optional[str] = Non
3030
format="%(asctime)s:%(levelname)s:%(name)s:%(filename)s:%(lineno)d:%(message)s",
3131
level=simulaqron_settings.log_level,
3232
force=True,
33-
stream=sys.stdout # send logs to the standard output, we set this earlier to be in /tmp
33+
stream=sys.stdout # send logs to the standard output, we set this earlier to be in /tmp
3434
)
3535

3636
# Give a way for the executioner to return messages

simulaqron/run/run.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ def run_applications(
193193
network_config.read_from_file(network_cfg)
194194
network_config.read_from_file(network_cfg)
195195

196-
197196
for _ in range(num_rounds):
198197
network = Network(
199198
nodes=network_config.get_node_names("default"),

simulaqron/settings/network_config.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,6 @@ def _check_socket_is_free(port: int) -> bool:
554554
return False
555555
return True
556556

557-
###########
558-
#
559557

560558
def get_default_network_config_file(use_embedded: bool = False) -> Path:
561559
"""

simulaqron/start/start_qnodeos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def setup_netqasm_server(my_name: str, netqasm_factory: NetQASMFactory):
9797
break
9898
except CannotListenError:
9999
logger.error(
100-
"START_QNODEOS: %s: NetQASM server address (%d) is already in use, trying again.",
100+
"START_QNODEOS: %s: NetQASM server address (%d) is already in use, trying again.",
101101
my_name, my_host.port
102102
)
103103
time.sleep(_RETRY_TIME)

simulaqron/virtual_node/virtual.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,3 @@ def __init__(self, fromName, toName, from_epr_socket_id, to_epr_socket_id, new_v
17921792
self.to_epr_socket_id = to_epr_socket_id
17931793
self.virt_num = new_virt_num
17941794
self.rawEntInfo = rawEntInfo
1795-
1796-
1797-

tests/quick/general/test_classical_sockets.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ def test_classical_communication(self):
3636
("Bob", TestClassicalSocket.bob_program_receiver),
3737
]
3838
)
39-
_ = run_applications(apps, network_cfg=get_default_network_config_file(use_embedded=True), use_app_config=False, enable_logging=False)
39+
_ = run_applications(
40+
apps,
41+
network_cfg=get_default_network_config_file(use_embedded=True),
42+
use_app_config=False,
43+
enable_logging=False
44+
)
4045

4146
def test_unknown_local(self):
4247
with pytest.raises(ValueError) as ex:

tests/quick/netqasm/test_free_qubit.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ def test_too_many_qubits(self):
102102
]
103103
)
104104
with pytest.raises(RuntimeError) as exc:
105-
_ = run_applications(apps, network_cfg=get_default_network_config_file(use_embedded=True), use_app_config=False, enable_logging=False)
105+
_ = run_applications(
106+
apps,
107+
network_cfg=get_default_network_config_file(use_embedded=True),
108+
use_app_config=False,
109+
enable_logging=False
110+
)
106111
assert "Virtual address 2 is outside the unit module (app ID 0) which has length 2" in str(exc.value)
107112

108113
def test_release_qubit(self):
@@ -111,7 +116,12 @@ def test_release_qubit(self):
111116
("Alice", TestFreeQubit.release_qubit)
112117
]
113118
)
114-
result = run_applications(apps, network_cfg=get_default_network_config_file(use_embedded=True), use_app_config=False, enable_logging=False)
119+
result = run_applications(
120+
apps,
121+
network_cfg=get_default_network_config_file(use_embedded=True),
122+
use_app_config=False,
123+
enable_logging=False
124+
)
115125
assert result[0]["app_Alice"] == 1
116126

117127
def test_release_qubit_b(self):
@@ -120,7 +130,12 @@ def test_release_qubit_b(self):
120130
("Alice", TestFreeQubit.release_qubit_b)
121131
]
122132
)
123-
result = run_applications(apps, network_cfg=get_default_network_config_file(use_embedded=True), use_app_config=False, enable_logging=False)
133+
result = run_applications(
134+
apps,
135+
network_cfg=get_default_network_config_file(use_embedded=True),
136+
use_app_config=False,
137+
enable_logging=False
138+
)
124139
assert result[0]["app_Alice"] == 1
125140

126141
def test_release_and_reuse_qubit(self):
@@ -129,7 +144,12 @@ def test_release_and_reuse_qubit(self):
129144
("Alice", TestFreeQubit.release_and_reuse_qubit)
130145
]
131146
)
132-
result = run_applications(apps, network_cfg=get_default_network_config_file(use_embedded=True), use_app_config=False, enable_logging=False)
147+
result = run_applications(
148+
apps,
149+
network_cfg=get_default_network_config_file(use_embedded=True),
150+
use_app_config=False,
151+
enable_logging=False
152+
)
133153
assert result[0]["app_Alice"] == 2
134154

135155
def test_release_and_reuse_qubit_b(self):
@@ -138,5 +158,10 @@ def test_release_and_reuse_qubit_b(self):
138158
("Alice", TestFreeQubit.release_and_reuse_qubit_b)
139159
]
140160
)
141-
result = run_applications(apps, network_cfg=get_default_network_config_file(use_embedded=True), use_app_config=False, enable_logging=False)
161+
result = run_applications(
162+
apps,
163+
network_cfg=get_default_network_config_file(use_embedded=True),
164+
use_app_config=False,
165+
enable_logging=False
166+
)
142167
assert result[0]["app_Alice"] == 2

0 commit comments

Comments
 (0)