Skip to content

Commit 6ee1b6b

Browse files
committed
Added support for several profiles per provider. Got rid of some errors.
1 parent 3c7d272 commit 6ee1b6b

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

mpqp/config.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import json as __json
2+
import platform as __platform
3+
import os as __os
4+
import sys as __sys
5+
import logging as __logging
6+
import gnupg as __gnupg
7+
8+
9+
__logger = __logging.getLogger(__name__)
10+
__logger.setLevel(__logging.DEBUG)
11+
12+
handler = __logging.StreamHandler(__sys.stderr)
13+
handler.setLevel(__logging.DEBUG)
14+
formatter = __logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
15+
handler.setFormatter(formatter)
16+
17+
# Comment out this line below if you do no more want any logging messages
18+
__logger.addHandler(handler)
19+
20+
# __logger.removeHandler(handler)
21+
22+
DEFAULT_CONFIG_FILE = [
23+
(
24+
__os.path.join(__os.environ["USERPROFILE"], ".config", "mpqp.json.gpg")
25+
if "Windows" in __platform.system()
26+
else __os.path.join(
27+
(
28+
__os.environ["XDG_CONFIG_HOME"]
29+
if "XDG_CONFIG_HOME" in __os.environ.keys()
30+
else __os.environ["HOME"]
31+
),
32+
"mpqp.gpg",
33+
)
34+
),
35+
(
36+
__os.path.join(__os.environ["USERPROFILE"], ".mpqp.json.gpg")
37+
if "Windows" in __platform.system()
38+
else __os.path.join(__os.environ["HOME"], ".mpqp.json.gpg")
39+
),
40+
(
41+
__os.path.join(__os.environ["USERPROFILE"], ".config", "mpqp.json")
42+
if "Windows" in __platform.system()
43+
else __os.path.join(
44+
(
45+
__os.environ["XDG_CONFIG_HOME"]
46+
if "XDG_CONFIG_HOME" in __os.environ.keys()
47+
else __os.environ["HOME"]
48+
),
49+
"mpqp.json",
50+
)
51+
),
52+
(
53+
__os.path.join(__os.environ["USERPROFILE"], ".mpqp.json")
54+
if "Windows" in __platform.system()
55+
else __os.path.join(__os.environ["HOME"], ".mpqp.json")
56+
),
57+
]
58+
59+
__logger.info("Default config file list set to " + str(DEFAULT_CONFIG_FILE))
60+
61+
62+
data = None
63+
ibm = None
64+
aws = None
65+
qlm = None
66+
67+
68+
def load(configFile: str | None = None):
69+
if configFile is None:
70+
for f in DEFAULT_CONFIG_FILE:
71+
__logger.info("Trying file \"%s\"" % f)
72+
if __os.path.isfile(f):
73+
configFile = f
74+
break
75+
76+
if configFile is not None:
77+
configStr: str | None = None
78+
79+
if configFile.endswith("gpg"):
80+
gpg = __gnupg.GPG(use_agent=True)
81+
gpg.encoding = "utf-8"
82+
decryptData = gpg.decrypt_file(open(configFile, mode="rb"), passphrase=None)
83+
84+
if decryptData.ok: # pyright: ignore[reportAttributeAccessIssue]
85+
configStr = str(decryptData)
86+
else:
87+
__logger.error(
88+
"Decryption failed with message \"%s\""
89+
% decryptData.status # pyright: ignore[reportAttributeAccessIssue]
90+
)
91+
else:
92+
with open(configFile, mode="rb") as fi:
93+
configStr = fi.read()
94+
95+
if configStr is not None:
96+
global data, ibm, aws, qlm
97+
data = __json.loads(configStr)
98+
ibm = data['IBM'] if 'IBM' in data.keys() else None
99+
aws = data['AWS'] if 'AWS' in data.keys() else None
100+
qlm = data['QLM'] if 'QLM' in data.keys() else None
101+
102+
103+
use_profile = None

mpqp/execution/connection/ibm_connection.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,28 @@ def get_QiskitRuntimeService() -> "QiskitRuntimeService":
112112
113113
"""
114114
from qiskit_ibm_runtime import QiskitRuntimeService
115+
from mpqp import config
115116

116117
global Runtime_Service
117118
if Runtime_Service is None:
118-
if get_env_variable("IBM_CONFIGURED") == "False":
119+
if get_env_variable("IBM_CONFIGURED") == "False" and config.data == None:
119120
raise IBMRemoteExecutionError(
120121
"Error when instantiating QiskitRuntimeService. No IBM account configured."
121122
)
122123
try:
123-
Runtime_Service = QiskitRuntimeService(channel="ibm_quantum")
124+
proxy = None
125+
126+
if "proxy" in config.ibm[config.use_profile].keys():
127+
proxy = {}
128+
for key in ["urls", "username_ntlm", "password_ntlm"]:
129+
if key in config.ibm[config.use_profile]['proxy'].keys():
130+
proxy[key] = config.ibm[config.use_profile]['proxy'][key]
131+
132+
Runtime_Service = QiskitRuntimeService(
133+
channel="ibm_quantum",
134+
token=config.ibm[config.use_profile]['token'],
135+
proxies=proxy,
136+
)
124137
except Exception as err:
125138
raise IBMRemoteExecutionError(
126139
"Error when instantiating QiskitRuntimeService (probably wrong token saved "

pyrightconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"reportUnknownMemberType": "none",
88
"reportUnknownLambdaType": "none",
99
"reportMissingTypeStubs": "none",
10+
"reportUnusedVariable": "none",
1011
"reportUnknownParameterType": "none",
12+
"reportOptionalSubscript": "warning",
1113
"reportUnnecessaryTypeIgnoreComment": "warning",
1214
"ignore": ["__pycache__/", "build/"]
1315
}

0 commit comments

Comments
 (0)