Skip to content

Commit

Permalink
Merge branch 'main' into adding-fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
skushnir123 committed Dec 22, 2023
2 parents 40acfec + ae54c3e commit 52d09c6
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 18 deletions.
9 changes: 0 additions & 9 deletions .github/CODEOWNERS

This file was deleted.

4 changes: 3 additions & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ jobs:
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -r docs/requirements.txt
python3 -m pip install -r requirements.txt
python3 -m pip install -e .
- name: Build docs
run: |
sphinx-build -W -b html docs/ docs/build/html
cd docs
sphinx-build -W -b html . build/html
2 changes: 2 additions & 0 deletions docs/api/qbraid_qir.cirq.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

qbraid_qir.cirq
=================

Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sphinx~=7.2.6
sphinx-autodoc-typehints>=1.24,<1.26
sphinx-rtd-theme~=1.3.0
sphinx-rtd-theme~=2.0.0
docutils<0.21
2 changes: 1 addition & 1 deletion docs/userguide/cirq_qir.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Convert a ``Cirq`` circuit to ``QIR`` code:
.. code-block:: python
import cirq
from qbraid.cirq_qir import cirq_to_qir
from qbraid_qir.cirq import cirq_to_qir
# Create two qubits
q0, q1 = cirq.LineQubit.range(2)
Expand Down
9 changes: 9 additions & 0 deletions qbraid_qir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
.. currentmodule:: qbraid_qir
Functions
-----------
.. autosummary::
:toctree: ../stubs/
cirq_to_qir
Exceptions
-----------
Expand All @@ -23,4 +31,5 @@
"""
from ._version import __version__
from .cirq import cirq_to_qir
from .exceptions import QirConversionError
19 changes: 19 additions & 0 deletions qbraid_qir/cirq/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,32 @@
from typing import Optional

import cirq
import qbraid.programs.cirq
from pyqir import Context, Module, qir_module

from qbraid_qir.cirq.elements import CirqModule, generate_module_id
from qbraid_qir.cirq.visitor import BasicQisVisitor
from qbraid_qir.exceptions import QirConversionError


def _preprocess_circuit(circuit: cirq.Circuit) -> cirq.Circuit:
"""
Preprocesses a Cirq circuit to ensure that it is compatible with the QIR conversion.
Args:
circuit (cirq.Circuit): The Cirq circuit to preprocess.
Returns:
cirq.Circuit: The preprocessed Cirq circuit.
"""
# circuit = cirq.contrib.qasm_import.circuit_from_qasm(circuit.to_qasm()) # decompose?
qprogram = qbraid.programs.cirq.CirqCircuit(circuit)
qprogram._convert_to_line_qubits()
cirq_circuit = qprogram.program
return cirq_circuit


def cirq_to_qir(circuit: cirq.Circuit, name: Optional[str] = None, **kwargs) -> Module:
"""
Converts a Cirq circuit to a PyQIR module.
Expand Down
1 change: 1 addition & 0 deletions qbraid_qir/cirq/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def visit_register(self, qids: List[cirq.Qid]):
_log.debug(
f"Added label for qubits {qids}"
)
_log.debug(f"Added label for qubits {qids}")

def process_composite_operation(self, operation: cirq.Operation):
# e.g. operation.gate.sub_gate, this functionality might exist elsewhere.
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pyqir~=0.10.0
qbraid==0.5.0.dev20231129222445
qbraid==0.5.0.dev20231213012035
cirq-core
2 changes: 1 addition & 1 deletion tests/fixtures/basic_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,4 @@ def test_fixture():
rotation_tests = [_fixture_name(s) for s in _rotations.keys()]
double_op_tests = [_fixture_name(s) for s in _two_qubit_gates.keys()]
triple_op_tests = [_fixture_name(s) for s in _three_qubit_gates.keys()]
measurement_tests = [_fixture_name(s) for s in _measurements.keys()]
measurement_tests = [_fixture_name(s) for s in _measurements.keys()]
6 changes: 3 additions & 3 deletions tests/test_cirq_to_qir.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
Module containing unit tests for Cirq to QIR conversion functions.
"""
import hashlib

import cirq
import pytest

from tests.fixtures.basic_gates import single_op_tests
import tests.test_utils as test_utils
from qbraid_qir.cirq.convert import cirq_to_qir, generate_module_id
from qbraid_qir.exceptions import QirConversionError
from tests.fixtures.basic_gates import single_op_tests

from .qir_utils import assert_equal_qir

Expand All @@ -38,6 +36,8 @@ def test_cirq_to_qir_conversion_error():
with pytest.raises(QirConversionError):
cirq_to_qir(circuit)


@pytest.mark.skip(reason="Not implemented yet")
@pytest.mark.parametrize("circuit_name", single_op_tests)
def test_single_qubit_gates(circuit_name, request):
qir_op, circuit = request.getfixturevalue(circuit_name)
Expand Down
58 changes: 58 additions & 0 deletions tests/test_preprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright (C) 2023 qBraid
#
# This file is part of the qBraid-SDK
#
# The qBraid-SDK is free software released under the GNU General Public License v3
# or later. You can redistribute and/or modify it under the terms of the GPL v3.
# See the LICENSE file in the project root or <https://www.gnu.org/licenses/gpl-3.0.html>.
#
# THERE IS NO WARRANTY for the qBraid-SDK, as per Section 15 of the GPL v3.
"""
Test functions that preprocess Cirq circuits before conversion to QIR.
"""
import cirq
import numpy as np
import pytest

from qbraid_qir.cirq.convert import _preprocess_circuit


@pytest.fixture
def gridqubit_circuit():
qubits = [cirq.GridQubit(x, 0) for x in range(4)]
circuit = cirq.Circuit(cirq.H(q) for q in qubits)
yield circuit


@pytest.fixture
def namedqubit_circuit():
qubits = [cirq.NamedQubit(f"q{i}") for i in range(4)]
circuit = cirq.Circuit(cirq.H(q) for q in qubits)
yield circuit


def test_convert_gridqubits_to_linequbits(gridqubit_circuit):
linequbit_circuit = _preprocess_circuit(gridqubit_circuit)
for qubit in linequbit_circuit.all_qubits():
assert isinstance(qubit, cirq.LineQubit), "Qubit is not a LineQubit"
assert np.allclose(
linequbit_circuit.unitary(), gridqubit_circuit.unitary()
), "Circuits are not equal"


def test_convert_namedqubits_to_linequbits(namedqubit_circuit):
linequbit_circuit = _preprocess_circuit(namedqubit_circuit)
for qubit in linequbit_circuit.all_qubits():
assert isinstance(qubit, cirq.LineQubit), "Qubit is not a LineQubit"
assert np.allclose(
linequbit_circuit.unitary(), namedqubit_circuit.unitary()
), "Circuits are not equal"


def test_empty_circuit_conversion():
circuit = cirq.Circuit()
converted_circuit = _preprocess_circuit(circuit)
assert (
len(converted_circuit.all_qubits()) == 0
), "Converted empty circuit should have no qubits"
16 changes: 15 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# Copyright (C) 2023 qBraid
#
# This file is part of the qBraid-SDK
#
# The qBraid-SDK is free software released under the GNU General Public License v3
# or later. You can redistribute and/or modify it under the terms of the GPL v3.
# See the LICENSE file in the project root or <https://www.gnu.org/licenses/gpl-3.0.html>.
#
# THERE IS NO WARRANTY for the qBraid-SDK, as per Section 15 of the GPL v3.
"""
Module containing unit tests Cirq to QIR utility/helper functions
"""
from typing import List

from pyqir import is_entry_point, Module, Function, Context
from pyqir import Context, Function, Module, is_entry_point


def _qubit_string(qubit: int) -> str:
Expand All @@ -23,6 +36,7 @@ def get_entry_point(mod: Module) -> Function:
assert func is not None, "No main function found"
return func


def get_entry_point_body(qir: List[str]) -> List[str]:
joined = "\n".join(qir)
mod = Module.from_ir(Context(), joined)
Expand Down

0 comments on commit 52d09c6

Please sign in to comment.