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

fix: Valid instructions will no longer be rejected by the compatibility layer #1742

Merged
merged 5 commits into from
Feb 22, 2024
Merged
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
42 changes: 18 additions & 24 deletions pyquil/external/rpcq.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import json
from typing import Dict, List, Union, Optional, Any, Literal
from typing import Dict, List, Union, Optional, Literal

from deprecated.sphinx import deprecated
from rpcq.messages import TargetDevice as TargetQuantumProcessor
from dataclasses import dataclass, field

JsonValue = Union[type(None), bool, int, float, str, List["JsonValue"], Dict[str, "JsonValue"]]


@dataclass
class Operator:
operator: Optional[str] = None
Expand All @@ -27,7 +28,7 @@ def _dict(self) -> Dict[str, JsonValue]:
)

@deprecated(
version="4.7",
version="4.6.2",
MarquessV marked this conversation as resolved.
Show resolved Hide resolved
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def dict(self):
Expand All @@ -39,7 +40,7 @@ def _parse_obj(cls, dictionary: Dict):

@classmethod
@deprecated(
version="4.7",
version="4.6.2",
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def parse_obj(cls, dictionary: Dict):
Expand Down Expand Up @@ -84,7 +85,7 @@ def _parse_obj(cls, dictionary: Dict) -> "MeasureInfo":

@classmethod
@deprecated(
version="4.7",
version="4.6.2",
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def parse_obj(cls, dictionary: Dict):
Expand All @@ -108,7 +109,7 @@ def _dict(self) -> Dict[str, JsonValue]:
)

@deprecated(
version="4.7",
version="4.6.2",
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def dict(self):
Expand All @@ -126,7 +127,7 @@ def _parse_obj(cls, dictionary: dict) -> "GateInfo":

@classmethod
@deprecated(
version="4.7",
version="4.6.2",
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def parse_obj(cls, dictionary: Dict):
Expand All @@ -149,16 +150,13 @@ class Qubit:
gates: List[Union[GateInfo, MeasureInfo]] = field(default_factory=list)

def _dict(self) -> Dict[str, JsonValue]:
encoding = dict(
id=self.id,
gates=[g._dict() for g in self.gates]
)
encoding = dict(id=self.id, gates=[g._dict() for g in self.gates])
if self.dead:
encoding["dead"] = self.dead
return encoding

@deprecated(
version="4.7",
version="4.6.2",
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def dict(self):
Expand All @@ -174,7 +172,7 @@ def _parse_obj(cls, dictionary: Dict) -> "Qubit":

@classmethod
@deprecated(
version="4.7",
version="4.6.2",
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def parse_obj(cls, dictionary: Dict):
Expand All @@ -188,16 +186,13 @@ class Edge:
gates: List[GateInfo] = field(default_factory=list)

def _dict(self) -> Dict[str, JsonValue]:
encoding = dict(
ids=self.ids,
gates=[g._dict() for g in self.gates]
)
encoding = dict(ids=self.ids, gates=[g._dict() for g in self.gates])
if self.dead:
encoding["dead"] = self.dead
return encoding

@deprecated(
version="4.7",
version="4.6.2",
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def dict(self):
Expand All @@ -208,12 +203,12 @@ def _parse_obj(cls, dictionary: dict) -> "Edge":
return Edge(
ids=dictionary["ids"],
dead=bool(dictionary.get("dead")),
gates=[GateInfo._parse_obj(g) for g in dictionary.get("gates", [])]
gates=[GateInfo._parse_obj(g) for g in dictionary.get("gates", [])],
)

@classmethod
@deprecated(
version="4.7",
version="4.6.2",
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def parse_obj(cls, dictionary: Dict):
Expand All @@ -228,11 +223,11 @@ class CompilerISA:
def _dict(self) -> Dict[str, JsonValue]:
return {
"1Q": {k: q._dict() for k, q in self.qubits.items()},
"2Q": {k: e._dict() for k, e in self.edges.items()}
"2Q": {k: e._dict() for k, e in self.edges.items()},
}

@deprecated(
version="4.7",
version="4.6.2",
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def dict(self):
Expand All @@ -249,15 +244,15 @@ def _parse_obj(cls, dictionary: Dict):

@classmethod
@deprecated(
version="4.7",
version="4.6.2",
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def parse_obj(cls, dictionary: Dict):
return cls._parse_obj(dictionary)

@classmethod
@deprecated(
version="4.7",
version="4.6.2",
reason="No longer requires serialization of RPCQ objects and is dropping Pydantic as a dependency.", # noqa: E501
)
def parse_file(cls, filename: str):
Expand All @@ -266,7 +261,6 @@ def parse_file(cls, filename: str):
return cls._parse_obj(json_dict)



def add_qubit(quantum_processor: CompilerISA, node_id: int) -> Qubit:
if node_id not in quantum_processor.qubits:
quantum_processor.qubits[str(node_id)] = Qubit(id=node_id)
Expand Down
54 changes: 36 additions & 18 deletions pyquil/quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def _convert_to_rs_instruction(instr: Union[AbstractInstruction, quil_rs.Instruc
return quil_rs.Instruction.from_gate_definition(instr)
if isinstance(instr, Halt):
return quil_rs.Instruction.new_halt()
if isinstance(instr, Include):
MarquessV marked this conversation as resolved.
Show resolved Hide resolved
return quil_rs.Instruction.from_include(instr)
if isinstance(instr, quil_rs.Load):
return quil_rs.Instruction.from_load(instr)
if isinstance(instr, quil_rs.MeasureCalibrationDefinition):
Expand Down Expand Up @@ -211,10 +213,18 @@ def _convert_to_py_instruction(instr: Any) -> AbstractInstruction:
return _convert_to_py_instruction(instr.inner())
if isinstance(instr, quil_rs.Arithmetic):
return ArithmeticBinaryOp._from_rs_arithmetic(instr)
if isinstance(instr, quil_rs.Capture):
return Capture._from_rs_capture(instr)
if isinstance(instr, quil_rs.BinaryLogic):
return LogicalBinaryOp._from_rs_binary_logic(instr)
if isinstance(instr, quil_rs.Calibration):
return DefCalibration._from_rs_calibration(instr)
if isinstance(instr, quil_rs.Capture):
return Capture._from_rs_capture(instr)
if isinstance(instr, quil_rs.CircuitDefinition):
return DefCircuit._from_rs_circuit_definition(instr)
if isinstance(instr, quil_rs.Convert):
return ClassicalConvert._from_rs_convert(instr)
if isinstance(instr, quil_rs.Comparison):
return ClassicalComparison._from_rs_comparison(instr)
if isinstance(instr, quil_rs.Declaration):
return Declare._from_rs_declaration(instr)
if isinstance(instr, quil_rs.Delay):
Expand All @@ -223,18 +233,36 @@ def _convert_to_py_instruction(instr: Any) -> AbstractInstruction:
if len(instr.qubits) > 0:
return DelayQubits._from_rs_delay(instr)
return Delay._from_rs_delay(instr)
if isinstance(instr, quil_rs.Exchange):
return ClassicalExchange._from_rs_exchange(instr)
if isinstance(instr, quil_rs.Fence):
if len(instr.qubits) == 0:
return FenceAll()
return Fence._from_rs_fence(instr)
if isinstance(instr, quil_rs.FrameDefinition):
return DefFrame._from_rs_frame_definition(instr)
if isinstance(instr, quil_rs.GateDefinition):
return DefGate._from_rs_gate_definition(instr)
if isinstance(instr, quil_rs.Gate):
return Gate._from_rs_gate(instr)
if isinstance(instr, quil_rs.Include):
return Include._from_rs_include(instr)
if isinstance(instr, quil_rs.Jump):
return Jump._from_rs_jump(instr)
if isinstance(instr, quil_rs.JumpWhen):
return JumpWhen._from_rs_jump_when(instr)
if isinstance(instr, quil_rs.JumpUnless):
return JumpUnless._from_rs_jump_unless(instr)
if isinstance(instr, quil_rs.Label):
return JumpTarget._from_rs_label(instr)
if isinstance(instr, quil_rs.Load):
return ClassicalLoad._from_rs_load(instr)
if isinstance(instr, quil_rs.MeasureCalibrationDefinition):
return DefMeasureCalibration._from_rs_measure_calibration_definition(instr)
if isinstance(instr, quil_rs.Measurement):
return Measurement._from_rs_measurement(instr)
if isinstance(instr, quil_rs.Move):
return ClassicalMove._from_rs_move(instr)
if isinstance(instr, quil_rs.Pragma):
return Pragma._from_rs_pragma(instr)
if isinstance(instr, quil_rs.Pulse):
Expand All @@ -246,12 +274,6 @@ def _convert_to_py_instruction(instr: Any) -> AbstractInstruction:
return Reset._from_rs_reset(instr)
else:
return ResetQubit._from_rs_reset(instr)
if isinstance(instr, quil_rs.CircuitDefinition):
return DefCircuit._from_rs_circuit_definition(instr)
if isinstance(instr, quil_rs.GateDefinition):
return DefGate._from_rs_gate_definition(instr)
if isinstance(instr, quil_rs.WaveformDefinition):
return DefWaveform._from_rs_waveform_definition(instr)
if isinstance(instr, quil_rs.SetFrequency):
return SetFrequency._from_rs_set_frequency(instr)
if isinstance(instr, quil_rs.SetPhase):
Expand All @@ -264,16 +286,12 @@ def _convert_to_py_instruction(instr: Any) -> AbstractInstruction:
return ShiftPhase._from_rs_shift_phase(instr)
if isinstance(instr, quil_rs.SwapPhases):
return SwapPhases._from_rs_swap_phases(instr)
if isinstance(instr, quil_rs.Label):
return JumpTarget._from_rs_label(instr)
if isinstance(instr, quil_rs.Move):
return ClassicalMove._from_rs_move(instr)
if isinstance(instr, quil_rs.Jump):
return Jump._from_rs_jump(instr)
if isinstance(instr, quil_rs.JumpWhen):
return JumpWhen._from_rs_jump_when(instr)
if isinstance(instr, quil_rs.JumpUnless):
return JumpUnless._from_rs_jump_unless(instr)
if isinstance(instr, quil_rs.Store):
return ClassicalStore._from_rs_store(instr)
if isinstance(instr, quil_rs.UnaryLogic):
return UnaryClassicalInstruction._from_rs_unary_logic(instr)
if isinstance(instr, quil_rs.WaveformDefinition):
return DefWaveform._from_rs_waveform_definition(instr)
if isinstance(instr, quil_rs.Instruction):
raise NotImplementedError(f"The {type(instr)} Instruction hasn't been mapped to an AbstractInstruction yet.")
elif isinstance(instr, AbstractInstruction):
Expand Down
Loading
Loading