Skip to content

Commit 4ff31fe

Browse files
fix: problem with myqlm and s_dagger solved
1 parent c85ac49 commit 4ff31fe

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

mpqp/core/circuit.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -760,29 +760,29 @@ def inverse(self) -> QCircuit:
760760
The inverse circuit.
761761
762762
Examples:
763-
>>> c1 = QCircuit([S(0), CZ(0,1), H(1), Ry(4.56, 1)])
763+
>>> c1 = QCircuit([T(0), CZ(0,1), H(1), Ry(4.56, 1)])
764764
>>> print(c1) # doctest: +NORMALIZE_WHITESPACE
765765
┌───┐
766-
q_0: ┤ S ├─■──────────────────
766+
q_0: ┤ T ├─■──────────────────
767767
└───┘ │ ┌───┐┌──────────┐
768768
q_1: ──────■─┤ H ├┤ Ry(4.56) ├
769769
└───┘└──────────┘
770770
>>> print(c1.inverse()) # doctest: +NORMALIZE_WHITESPACE
771771
┌────┐
772-
q_0: ───────────────────■─┤ S† ├
772+
q_0: ───────────────────■─┤ T† ├
773773
┌───────────┐┌───┐ │ └────┘
774774
q_1: ┤ Ry(-4.56) ├┤ H ├─■───────
775775
└───────────┘└───┘
776-
>>> c2 = QCircuit([S(0), CRk(2, 0, 1), Barrier(), H(1), Ry(4.56, 1)])
776+
>>> c2 = QCircuit([T(0), CRk(2, 0, 1), Barrier(), H(1), Ry(4.56, 1)])
777777
>>> print(c2) # doctest: +NORMALIZE_WHITESPACE
778778
┌───┐ ░
779-
q_0: ┤ S ├─■────────░──────────────────
779+
q_0: ┤ T ├─■────────░──────────────────
780780
└───┘ │P(π/2) ░ ┌───┐┌──────────┐
781781
q_1: ──────■────────░─┤ H ├┤ Ry(4.56) ├
782782
░ └───┘└──────────┘
783783
>>> print(c2.inverse()) # doctest: +NORMALIZE_WHITESPACE
784784
░ ┌────┐
785-
q_0: ───────────────────░──■────────┤ S† ├
785+
q_0: ───────────────────░──■────────┤ T† ├
786786
┌───────────┐┌───┐ ░ │P(-π/2) └────┘
787787
q_1: ┤ Ry(-4.56) ├┤ H ├─░──■──────────────
788788
└───────────┘└───┘ ░

mpqp/core/instruction/gates/native_gates.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,10 @@ class OneQubitNoParamGate(SingleQubitGate, NoParameterGate, SimpleClassReprABC):
378378
target: Index referring to the qubits on which the gate will be applied.
379379
"""
380380

381-
def __init__(self, target: int):
382-
SingleQubitGate.__init__(self, target, type(self).__name__)
381+
def __init__(self, target: int, label: Optional[str] = None):
382+
SingleQubitGate.__init__(
383+
self, target, type(self).__name__ if label is None else label
384+
)
383385

384386

385387
class Id(OneQubitNoParamGate, InvolutionGate):
@@ -781,7 +783,7 @@ class S_dagger(OneQubitNoParamGate):
781783
782784
Example:
783785
>>> pprint(S_dagger(0).to_matrix())
784-
[[1, 0 ],
786+
[[1, 0 ],
785787
[0, -1j]]
786788
787789
"""
@@ -802,17 +804,17 @@ def qiskit_gate(cls):
802804
def cirq_gate(cls):
803805
from cirq.ops.common_gates import ZPowGate
804806

805-
return ZPowGate(exponent=-np.pi / 2)
807+
return ZPowGate(exponent=-1 / 2)
806808

807-
qlm_aqasm_keyword = "PH"
809+
qlm_aqasm_keyword = "DAG(S)"
808810
qiskit_string = "sdg"
809811

810812
def __init__(self, target: int):
811-
super().__init__(target)
813+
super().__init__(target, "S†")
812814
self.matrix = np.array([[1, 0], [0, -1j]])
813815

814816
def __repr__(self):
815-
return f"S†({self.targets[0]})"
817+
return f"{type(self).__name__}({self.targets[0]})"
816818

817819
def inverse(self) -> Gate:
818820
return S(self.targets[0])

mpqp/qasm/qasm_to_myqlm.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
call the function :func:`qasm2_to_myqlm_Circuit` to generate the circuit from
55
the qasm code."""
66

7+
import re
78
from typing import TYPE_CHECKING
89

10+
from mpqp.qasm.open_qasm_2_and_3 import open_qasm_hard_includes
911
from typeguard import typechecked
1012

1113
if TYPE_CHECKING:
1214
from qat.core.wrappers.circuit import Circuit
1315

14-
from mpqp.qasm.open_qasm_2_and_3 import open_qasm_hard_includes
15-
1616

1717
@typechecked
1818
def qasm2_to_myqlm_Circuit(qasm_str: str) -> "Circuit":
@@ -45,5 +45,10 @@ def qasm2_to_myqlm_Circuit(qasm_str: str) -> "Circuit":
4545
from qat.interop.openqasm import OqasmParser
4646

4747
parser = OqasmParser(gates={"p": "PH", "u": "U"}) # requires myqlm-interop-1.9.3
48-
circuit = parser.compile(open_qasm_hard_includes(qasm_str, set()))
48+
49+
# We replace 'sdg' for S_dagger gate by 'u1(pi/2)', because of problem on myqlm side
50+
pattern = re.compile(r'(?<!gate\s)sdg\s+(\S+)\s*;')
51+
updated_qasm = pattern.sub(r'u1(-pi/2) \1;', qasm_str)
52+
53+
circuit = parser.compile(open_qasm_hard_includes(updated_qasm, set()))
4954
return circuit

tests/core/instruction/gates/test_custom_gate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def test_custom_gate_with_native_gates(device: AvailableDevice):
107107
result2 = _run_single(c2, device, {})
108108

109109
# we reduce the precision because of approximation errors coming from CustomGate usage
110-
assert matrix_eq(result1.amplitudes, result2.amplitudes, 1e-5, 1e-5)
110+
assert matrix_eq(result1.amplitudes, result2.amplitudes, 1e-4, 1e-4)
111111

112112

113113
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)