Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: to_other_language cirq without QASM2.0 #119

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
44 changes: 39 additions & 5 deletions mpqp/core/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,12 +1134,46 @@ def to_other_language(
self.nb_qubits,
)
elif language == Language.CIRQ:
qasm2_code = self.to_other_language(Language.QASM2)
if TYPE_CHECKING:
assert isinstance(qasm2_code, str)
from mpqp.qasm.qasm_to_cirq import qasm2_to_cirq_Circuit
from cirq.circuits.circuit import Circuit as CirqCircuit
from cirq.ops.named_qubit import NamedQubit
from cirq.ops.identity import I

cirq_qubits = [NamedQubit(f"q_{i}") for i in range(self.nb_qubits)]
cirq_circuit = CirqCircuit()

for qubit in cirq_qubits:
hJaffaliColibritd marked this conversation as resolved.
Show resolved Hide resolved
cirq_circuit.append(I(qubit))

for instruction in self.instructions:
if isinstance(instruction, (ExpectationMeasure, Barrier, Breakpoint)):
continue
elif isinstance(instruction, CustomGate):
custom_circuit = QCircuit(self.nb_qubits)
custom_circuit.add(instruction)
qasm2_code = custom_circuit.to_other_language(Language.QASM2)
if TYPE_CHECKING:
assert isinstance(qasm2_code, str)
from mpqp.qasm.qasm_to_cirq import qasm2_to_cirq_Circuit

custom_cirq_circuit = qasm2_to_cirq_Circuit(qasm2_code)
cirq_circuit += custom_cirq_circuit
hJaffaliColibritd marked this conversation as resolved.
Show resolved Hide resolved
self.gphase += custom_circuit.gphase
elif isinstance(instruction, ControlledGate):
targets = []
for target in instruction.targets:
targets.append(cirq_qubits[target])
controls = []
for control in instruction.controls:
controls.append(cirq_qubits[control])
cirq_instruction = instruction.to_other_language(Language.CIRQ)
cirq_circuit.append(cirq_instruction.on(*controls, *targets))
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

Here we handle all the other cases ? (Parametrized gate, Barrier, BasisMeasure, ..) right ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Cirq doesn't have a Barrier operation, so we skips it. breakpoints is also skip. While the rest is handled properly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, even BasisMeasure ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The BasisMeasure is a simple measurement with a pre-measurement circuit, we juste have to concatenate it. so yes

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we concatenate this pre-measurement circuit (the one related with the basis change I suppose) somewhere? (because I checked the code, and I didn't found it).

But I ask myself if we should include that change of basis or not. I am not convinced personally, but if we do we have to warn the user. I would export the circuit without the change of basis additional unitary. What do you think ?

targets = []
for target in instruction.targets:
targets.append(cirq_qubits[target])
cirq_instruction = instruction.to_other_language(Language.CIRQ)
cirq_circuit.append(cirq_instruction.on(*targets))

cirq_circuit = qasm2_to_cirq_Circuit(qasm2_code)
if cirq_proc_id:
from cirq.transformers.optimize_for_target_gateset import (
optimize_for_target_gateset,
Expand Down
Loading
Loading