Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions mitiq/interface/mitiq_cudaq/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (C) Unitary Foundation
#
# This source code is licensed under the GPL license (v3) found in the
# LICENSE file in the root directory of this source tree.

from mitiq.interface.mitiq_cudaq.conversions import (
from_cudaq,
to_cudaq,
)
53 changes: 53 additions & 0 deletions mitiq/interface/mitiq_cudaq/conversions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (C) Unitary Foundation
#
# This source code is licensed under the GPL license (v3) found in the
# LICENSE file in the root directory of this source tree.

"""Functions to convert between Mitiq's internal circuit representation and
Cuda-Quantum's circuit representation.
"""

from cirq import Circuit
from cudaq import PyKernel
from qbraid.transpiler.conversions.cirq import cirq_to_qasm2
from qbraid.transpiler.conversions.cudaq import cudaq_to_qasm2
from qbraid.transpiler.conversions.openqasm3 import openqasm3_to_cudaq
from qbraid.transpiler.conversions.qasm2 import qasm2_to_cirq, qasm2_to_qiskit
from qbraid.transpiler.conversions.qasm3 import qasm3_to_openqasm3
from qbraid.transpiler.conversions.qiskit import qiskit_to_qasm3


def from_cudaq(kernel: PyKernel) -> Circuit:
"""Convert a cudaq PyKernel to a cirq Circuit via OpenQASM 3.

Args:
kernel (PyKernel): The cudaq PyKernel to convert.

Returns:
Circuit: The converted cirq Circuit.
"""
qasm2_str = cudaq_to_qasm2(kernel)
return qasm2_to_cirq(qasm2_str)


def to_cudaq(circuit: Circuit) -> PyKernel:
"""Convert a cirq Circuit to a cudaq PyKernel via OpenQASM 3.

Args:
circuit (Circuit): The cirq Circuit to convert.

Returns:
PyKernel: The converted cudaq PyKernel.
"""
from qiskit import transpile

qasm2_str = cirq_to_qasm2(circuit)
qiskit_qc = qasm2_to_qiskit(qasm2_str)
# We need to transpile as `openqasm3_to_cudaq` cannot work
# with arbitrary gates, i.e., ccx, rxx, etc.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It specifically only works with u3 and cx?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean qbraid's conversion, no, only single qubit gates and single controlled single qubit gates. Issue is that cudaq doesn't have ccx or mcx. They just do cx([0, 1, 2], [3]) for instance for mcx(replace indices there with qr[i] of course for correct syntax).

So, I thought it'd be better to just use u3 and cx when converting to cudaq side. It keeps maintenance easier, and would work for all custom gates.

qiskit_qc = transpile(qiskit_qc, basis_gates=["u3", "cx"])
qasm3_str = qiskit_to_qasm3(qiskit_qc)
openqasm3_str = qasm3_to_openqasm3(qasm3_str)
cudaq_qc = openqasm3_to_cudaq(openqasm3_str)

return cudaq_qc
Loading