Skip to content

Commit 85bde38

Browse files
Merge pull request #26 from ColibrITD-SAS/feat-capture-braket-hardprint
Catch hard print in Braket conversion
2 parents 9d42550 + b4f9701 commit 85bde38

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

mpqp/qasm/open_qasm_2_and_3.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
from enum import Enum
66

77
from warnings import warn
8-
from textwrap import dedent
98
from anytree import Node, PreOrderIter
109
from typeguard import typechecked
1110

12-
from mpqp.tools.errors import InstructionParsingError
11+
from mpqp.tools.errors import InstructionParsingError, OpenQASMTranslationWarning
1312

1413

1514
class Instr(Enum):
@@ -234,13 +233,13 @@ def add_std_lib():
234233
instructions_code += instr + ";\n"
235234
elif instr_name.lower() == "u":
236235
warn(
237-
dedent(
238-
"""OpenQASMTranslationWarning:
239-
There is a phase e^(i(a+c)/2) difference between U(a,b,c) gate in 2.0 and 3.0.
240-
We handled that for you by adding the extra phase at the right place.
241-
Be careful if you want to create a control gate from this circuit/gate, the
242-
phase can become non-global."""
243-
)
236+
(
237+
"\nThere is a phase e^(i(a+c)/2) difference between U(a,b,c) gate in 2.0 and 3.0.\n"
238+
"We handled that for you by adding the extra phase at the right place.\n"
239+
"Be careful if you want to create a control gate from this "
240+
"circuit/gate, the phase can become non-global."
241+
),
242+
OpenQASMTranslationWarning,
244243
)
245244
header_code += add_std_lib()
246245
instructions_code += "u3" + instr[1:] + ";\n"

mpqp/qasm/qasm_to_braket.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
"""File regrouping all features for translating QASM code to Amazon Braket objects."""
22

3-
from braket.ir.openqasm import Program
3+
import warnings
4+
from logging import Logger, getLogger
5+
46
from braket.circuits import Circuit
7+
from braket.ir.openqasm import Program
58
from typeguard import typechecked
69

710
from mpqp.qasm.open_qasm_2_and_3 import open_qasm_hard_includes
11+
from mpqp.tools.errors import UnsupportedBraketFeaturesWarning
812

913

1014
@typechecked
@@ -55,6 +59,25 @@ def qasm3_to_braket_Circuit(qasm3_str: str) -> Circuit:
5559
after_stdgates_included = open_qasm_hard_includes(qasm3_str, set())
5660
# NOTE : gphase is a already used in Braket and thus cannot be redefined as a native gate in OpenQASM.
5761
# We used ggphase instead
62+
warning_message = (
63+
"This program uses OpenQASM language features that may not be "
64+
"supported on QPUs or on-demand simulators."
65+
)
66+
67+
# handle the logger output
68+
# capture their logger
69+
braket_logger = getLogger()
70+
# add logger handler
5871

72+
logger_out = []
5973
circuit = Circuit.from_ir(after_stdgates_included)
74+
75+
if warning_message in logger_out:
76+
warnings.warn("\n" + warning_message, UnsupportedBraketFeaturesWarning)
77+
del logger_out[logger_out.index(warning_message)]
78+
# remove logger handler
79+
80+
for line in logger_out:
81+
braket_logger.warning(line)
82+
6083
return circuit

mpqp/tools/errors.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
class InstructionParsingError(ValueError):
2-
"""Raised when an QASM instruction encountered by the parser is malformed"""
2+
"""Raised when an QASM instruction encountered by the parser is malformed."""
33

44

55
class NumberQubitsError(ValueError):
6-
"""Raised when the number of qubits defining an instruction, a gate, or a measurement, is not coherent with the
7-
related objets (circuit, matrix, observable, etc.)"""
6+
"""Raised when the number of qubits defining an instruction, a gate, or a
7+
measurement, is not coherent with the related objects (circuit, matrix,
8+
observable, etc...)."""
89

910

1011
class ResultAttributeError(AttributeError):
11-
"""Raised when one tries to access the attribute of the result that is incoherent with the associated job."""
12+
"""Raised when one tries to access the attribute of the result that is
13+
incoherent with the associated job."""
1214

1315

1416
class DeviceJobIncompatibleError(ValueError):
15-
"""Raised when one tries to run a job with a JobType that is not suitable for the selected device
16-
(for example SAMPLE job on a statevector simulator)"""
17+
"""Raised when one tries to run a job with a JobType that is not suitable
18+
for the selected device (for example SAMPLE job on a statevector simulator)."""
1719

1820

1921
class RemoteExecutionError(ConnectionError):
20-
"""Raised when an error occurred during a remote connection, submission or execution"""
22+
"""Raised when an error occurred during a remote connection, submission or
23+
execution."""
2124

2225

2326
class IBMRemoteExecutionError(RemoteExecutionError):
24-
"""Raised when an error occurred during the remote execution process of job(s) on an IBM device"""
27+
"""Raised when an error occurred during the remote execution process of
28+
job(s) on an IBM device."""
2529

2630

2731
class QLMRemoteExecutionError(RemoteExecutionError):
28-
"""Raised when an error occurred during the remote execution process of job(s) on the remote QLM"""
32+
"""Raised when an error occurred during the remote execution process of
33+
job(s) on the remote QLM."""
2934

3035

3136
class AWSBraketRemoteExecutionError(RemoteExecutionError):
32-
"""Raised when an error occurred during the remote execution process of job(s) on the remote Amazon Braket"""
37+
"""Raised when an error occurred during the remote execution process of
38+
job(s) on the remote Amazon Braket."""
39+
40+
41+
class UnsupportedBraketFeaturesWarning(UserWarning):
42+
"""Warning for potential compatibility issues with Braket."""
43+
44+
45+
class OpenQASMTranslationWarning(UserWarning):
46+
"""Warning for potential translation error when exporting to OpenQASM."""

0 commit comments

Comments
 (0)