Skip to content

Commit

Permalink
Merge pull request #255 from oqc-community/improvement/ker/gate_suppo…
Browse files Browse the repository at this point in the history
…rt_checks

Improved test coverage for default QASM gates.
  • Loading branch information
keriksson-rosenqvist authored Nov 18, 2024
2 parents e9d2b16 + 26fbce3 commit 320ea30
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/qat/purr/backends/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def apply_setup_to_hardware(
frequency=5.5e9,
scale=0.0,
)
qubit.add_coupled_qubit(qubit_devices[(i + 1) % qubit_count])
qubit.add_coupled_qubit(qubit_devices[(i - 1) % qubit_count])
qubit.add_coupled_qubit(qubit_devices[(i + 1) % qubit_count])
qubit.add_coupled_qubit(qubit_devices[(i - 1) % qubit_count])
else:
for connection in connectivity:
left_index, right_index = connection
Expand Down
2 changes: 2 additions & 0 deletions src/qat/purr/integrations/qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,8 @@ def visit_QuantumGate(self, node: ast.QuantumGate, context: QasmContext):
case "u" | "U":
theta, phi, _lambda = arguments
return self.add_unitary(theta, phi, _lambda, target_qubits, self.builder)
case "cu" | "CU":
raise NotImplementedError("Controlled Unitary gate is not yet supported.")
case "cx" | "CX":
return self.add_cnot(*target_qubits, self.builder)
case "ecr" | "ECR":
Expand Down
42 changes: 41 additions & 1 deletion tests/qat/qasm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ def parse_and_apply_optimiziations(
return builder


qasm3_base = """
OPENQASM 3.0;
bit[{N}] c;
qubit[{N}] q;
{gate_strings}
measure q -> c;
"""


qasm3_gates = {}


Expand All @@ -83,10 +92,41 @@ def get_default_qasm3_gate_qasms():
arg_string = (
""
if needed_num_args == 0
else "(" + ", ".join(["0"] * needed_num_args) + ")"
else "(" + ", ".join(["1.2"] * needed_num_args) + ")"
)
N = len(defi.qubits)
qubit_string = ", ".join([f"q[{i}]" for i in range(N)])
gate_string = f"{name}{arg_string} {qubit_string};"
qasm3_gates[name] = (N, gate_string)
return list(qasm3_gates.values())


qasm2_base = """
OPENQASM 2.0;
include "qelib1.inc";
creg c[{N}];
qreg q[{N}];
{gate_strings}
measure q -> c;
"""


qasm2_gates = {}


def get_default_qasm2_gate_qasms():
if len(qasm2_gates) == 0:
intrinsics = Qasm2Parser()._get_intrinsics()
for defi in intrinsics:
name = defi.name
needed_num_args = defi.num_params
arg_string = (
""
if needed_num_args == 0
else "(" + ", ".join(["1.2"] * needed_num_args) + ")"
)
N = defi.num_qubits
qubit_string = ", ".join([f"q[{i}]" for i in range(N)])
gate_string = f"{name}{arg_string} {qubit_string};"
qasm2_gates[name] = (N, gate_string)
return list(qasm2_gates.values())
47 changes: 45 additions & 2 deletions tests/qat/test_qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@

from tests.qat.qasm_utils import (
ProgramFileType,
get_default_qasm2_gate_qasms,
get_default_qasm3_gate_qasms,
get_qasm2,
get_qasm3,
get_test_file_path,
parse_and_apply_optimiziations,
qasm2_base,
qasm3_base,
)
from tests.qat.utils.models import get_jagged_echo_hardware, update_qubit_indices

Expand Down Expand Up @@ -610,7 +613,9 @@ def test_sech(self):
def test_default_gates(self, gate_tup):
"""Check that each default gate can be parsed individually."""
N, gate_string = gate_tup
qasm = f"OPENQASM 3.0;\nbit[{N}] c;\nqubit[{N}] q;\n{gate_string}\nmeasure q -> c;"
if gate_string.startswith("cu("):
pytest.skip("CU gate is not yet supported.")
qasm = qasm3_base.format(N=N, gate_strings=gate_string)
hw = get_default_echo_hardware(max(N, 2))
parser = Qasm3Parser()
builder = parser.parse(hw.create_builder(), qasm)
Expand All @@ -622,8 +627,10 @@ def test_default_gates_together(self):
"""Check that all default gates can be parsed together."""
Ns, strings = zip(*get_default_qasm3_gate_qasms())
N = max(Ns)
# TODO: Remove filtering when CU gate is supported.
strings = filter(lambda s: not s.startswith("cu("), strings)
gate_strings = "\n".join(strings)
qasm = f"OPENQASM 3.0;\nbit[{N}] c;\nqubit[{N}] q;\n{gate_strings}\nmeasure q -> c;"
qasm = qasm3_base.format(N=N, gate_strings=gate_strings)
hw = get_default_echo_hardware(max(N, 2))
parser = Qasm3Parser()
builder = parser.parse(hw.create_builder(), qasm)
Expand Down Expand Up @@ -1125,6 +1132,42 @@ def test_on_jagged_hardware(self, qasm_file):
result = parser.parse(get_builder(hw), qasm_string)
assert len(result.instructions) > 0

# TODO: Remove gates from list as support is added.
_unsupported_gates = ("id", "u0", "rc3x", "c3x", "c3sqrtx", "c4x", "delay")

@pytest.mark.parametrize(
"gate_tup", get_default_qasm2_gate_qasms(), ids=lambda val: val[-1]
)
def test_default_gates(self, gate_tup):
"""Check that each default gate can be parsed individually."""
N, gate_string = gate_tup
if gate_string.startswith(self._unsupported_gates):
pytest.skip("Gate not yet supported.")
qasm = qasm2_base.format(N=N, gate_strings=gate_string)
hw = get_default_echo_hardware(max(N, 2))
parser = Qasm2Parser()
builder = parser.parse(hw.create_builder(), qasm)
assert isinstance(builder, InstructionBuilder)
assert len(builder.instructions) > 0
assert isinstance(builder.instructions[-1], Return)

def test_default_gates_together(self):
"""Check that all default gates can be parsed together."""
Ns, strings = zip(*get_default_qasm2_gate_qasms())
N = max(Ns)
# TODO: Remove filtering when all gates are supported.
strings = filter(lambda s: not s.startswith(self._unsupported_gates), strings)
gate_strings = "\n".join(strings)
qasm = qasm2_base.format(N=N, gate_strings=gate_strings)
hw = get_default_echo_hardware(
N, [(i, j) for i in range(N) for j in range(i, N) if i != j]
)
parser = Qasm2Parser()
builder = parser.parse(hw.create_builder(), qasm)
assert isinstance(builder, InstructionBuilder)
assert len(builder.instructions) > 0
assert isinstance(builder.instructions[-1], Return)


class TestQatOptimization:
def _measure_merge_timings(self, file, qubit_count, keys, expected):
Expand Down

0 comments on commit 320ea30

Please sign in to comment.