Skip to content

Commit

Permalink
feat: Update quil to >=0.13.2
Browse files Browse the repository at this point in the history
  • Loading branch information
MarquessV committed Nov 12, 2024
1 parent 8eb4b97 commit 7026c25
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 305 deletions.
57 changes: 29 additions & 28 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ scipy = "^1.11"
rpcq = "^3.11.0"
networkx = ">=2.5"
qcs-sdk-python = ">=0.20.1"
quil = ">=0.11.2"
quil = ">=0.13.2"
packaging = "^23.1"
deprecated = "^1.2.14"
types-deprecated = "^1.2.9.3"
Expand Down
58 changes: 35 additions & 23 deletions pyquil/quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2772,41 +2772,40 @@ def __new__(
"""Initialize a new calibration definition."""
return super().__new__(
cls,
name,
_convert_to_rs_expressions(parameters),
_convert_to_rs_qubits(qubits),
quil_rs.CalibrationIdentifier(
name,
_convert_to_rs_expressions(parameters),
_convert_to_rs_qubits(qubits),
modifiers or [],
),
_convert_to_rs_instructions(instrs),
modifiers or [],
)

@classmethod
def _from_rs_calibration(cls, calibration: quil_rs.Calibration) -> "DefCalibration":
return super().__new__(
cls,
calibration.name,
calibration.parameters,
calibration.qubits,
calibration.instructions,
calibration.modifiers,
)
return super().__new__(cls, calibration.identifier, calibration.instructions)

@property # type: ignore[override]
def parameters(self) -> Sequence[ParameterDesignator]:
"""The parameters of the calibration."""
return _convert_to_py_expressions(super().parameters)
return _convert_to_py_expressions(super().parameters())

@parameters.setter
def parameters(self, parameters: Sequence[ParameterDesignator]) -> None:
quil_rs.Calibration.parameters.__set__(self, _convert_to_rs_expressions(parameters)) # type: ignore[attr-defined] # noqa
identifier = super().identifier
identifier.parameters = _convert_to_rs_expressions(parameters)
quil_rs.Calibration.identifier.__set__(self, identifier) # type: ignore[attr-defined] # noqa

@property # type: ignore[override]
def qubits(self) -> list[QubitDesignator]:
"""The qubits the calibration operates on."""
return _convert_to_py_qubits(super().qubits)
return _convert_to_py_qubits(super().identifier.qubits)

@qubits.setter
def qubits(self, qubits: Sequence[QubitDesignator]) -> None:
quil_rs.Calibration.qubits.__set__(self, _convert_to_rs_qubits(qubits)) # type: ignore[attr-defined]
identifier = super().identifier
identifier.qubits = _convert_to_rs_qubits(qubits)
quil_rs.Calibration.identifier.__set__(self, identifier) # type: ignore[attr-defined]

@property
def instrs(self) -> list[AbstractInstruction]:
Expand All @@ -2826,6 +2825,16 @@ def instructions(self) -> list[AbstractInstruction]:
def instructions(self, instructions: list[AbstractInstruction]) -> None:
self.instrs = instructions

@property
def name(self) -> str:
return super().identifier.name

@name.setter
def name(self, name: str) -> None:
identifier = super().identifier
identifier.name = name
quil_rs.Calibration.identifier.__set__(self, identifier) # type: ignore

def out(self) -> str:
"""Return the instruction as a valid Quil string."""
return super().to_quil()
Expand Down Expand Up @@ -2854,37 +2863,40 @@ def __new__(
rs_qubit = None if not qubit else _convert_to_rs_qubit(qubit)
return super().__new__(
cls,
rs_qubit,
memory_reference.name,
quil_rs.MeasureCalibrationIdentifier(rs_qubit, str(memory_reference)),
_convert_to_rs_instructions(instrs),
)

@classmethod
def _from_rs_measure_calibration_definition(
cls, calibration: quil_rs.MeasureCalibrationDefinition
) -> "DefMeasureCalibration":
return super().__new__(cls, calibration.qubit, calibration.parameter, calibration.instructions)
return super().__new__(cls, calibration.identifier, calibration.instructions)

@property # type: ignore[override]
def qubit(self) -> Optional[QubitDesignator]:
"""Get the qubit this calibration matches."""
qubit = super().qubit
qubit = super().identifier.qubit
if not qubit:
return None
return _convert_to_py_qubit(qubit)

@qubit.setter
def qubit(self, qubit: QubitDesignator) -> None:
quil_rs.MeasureCalibrationDefinition.qubit.__set__(self, _convert_to_rs_qubit(qubit)) # type: ignore[attr-defined] # noqa
identifier = super().identifier
identifier.qubit = _convert_to_rs_qubit(qubit)
quil_rs.MeasureCalibrationDefinition.identifier.__set__(self, identifier) # type: ignore[attr-defined] # noqa

@property
def memory_reference(self) -> Optional[MemoryReference]:
"""Get the memory reference this calibration writes to."""
return MemoryReference._from_parameter_str(super().parameter)
return MemoryReference._from_parameter_str(super().identifier.parameter)

@memory_reference.setter
def memory_reference(self, memory_reference: MemoryReference) -> None:
quil_rs.MeasureCalibrationDefinition.parameter.__set__(self, memory_reference.name) # type: ignore[attr-defined] # noqa
identifier = super().identifier
identifier.parameter = memory_reference.out()
quil_rs.MeasureCalibrationDefinition.identifier.__set__(self, identifier) # type: ignore[attr-defined] # noqa

@property
def instrs(self) -> list[AbstractInstruction]:
Expand Down
8 changes: 4 additions & 4 deletions pyquil/quiltcalibrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ def _convert_to_calibration_match(
if isinstance(instruction, quil_rs.Gate) and isinstance(calibration, quil_rs.Calibration):
target_qubits = instruction.qubits
target_values: Sequence[Union[quil_expr.Expression, MemoryReference]] = instruction.parameters
parameter_qubits = calibration.qubits
parameter_values: Sequence[Union[quil_expr.Expression, MemoryReference]] = calibration.parameters
parameter_qubits = calibration.qubits()
parameter_values: Sequence[Union[quil_expr.Expression, MemoryReference]] = calibration.parameters()
py_calibration: Union[DefCalibration, DefMeasureCalibration] = DefCalibration._from_rs_calibration(calibration)
elif isinstance(instruction, quil_rs.Measurement) and isinstance(calibration, quil_rs.MeasureCalibrationDefinition):
target_qubits = [instruction.qubit]
target_values = (
[] if not instruction.target else [MemoryReference._from_rs_memory_reference(instruction.target)]
)
parameter_qubits = [] if not calibration.qubit else [calibration.qubit]
parameter_values = [MemoryReference._from_parameter_str(calibration.parameter)]
parameter_qubits = [] if not calibration.qubit() else [calibration.qubit()]
parameter_values = [MemoryReference._from_parameter_str(calibration.identifier.parameter)]
py_calibration = DefMeasureCalibration._from_rs_measure_calibration_definition(calibration)
else:
return None
Expand Down
4 changes: 2 additions & 2 deletions test/unit/__snapshots__/test_main.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# name: test_remove_reset_from_program
'''
DEFGATE XGATE AS MATRIX:
0, 1
1, 0
0, 1
1, 0

X 0

Expand Down
6 changes: 3 additions & 3 deletions test/unit/__snapshots__/test_program.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
DECLARE ro BIT[1]
DECLARE shot_count INTEGER[1]
DEFFRAME 0 "rx":
HARDWARE-OBJECT: "hardware"
HARDWARE-OBJECT: "hardware"
DEFWAVEFORM custom:
1, 2
1, 2
DEFCAL I 0:
DELAY 0 1
DELAY 0 1
MOVE shot_count[0] 100
LABEL @start-loop
MEASURE q ro[0]
Expand Down
62 changes: 35 additions & 27 deletions test/unit/__snapshots__/test_quil.ambr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# serializer version: 1
# name: TestProgram.test_calibrations
list([
Calibration { instructions: [Gate(Gate { name: "X", parameters: [], qubits: [Fixed(0)], modifiers: [] })], modifiers: [], name: "Calibrate", parameters: [], qubits: [Fixed(0)] },
Calibration { instructions: [Gate(Gate { name: "Y", parameters: [], qubits: [Fixed(1)], modifiers: [] })], modifiers: [], name: "Reticulating-Splines", parameters: [Variable("Spline")], qubits: [Fixed(1)] },
Calibration { identifier: CalibrationIdentifier { modifiers: [], name: "Calibrate", parameters: [], qubits: [Fixed(0)] }, instructions: [Gate(Gate { name: "X", parameters: [], qubits: [Fixed(0)], modifiers: [] })] },
Calibration { identifier: CalibrationIdentifier { modifiers: [], name: "Reticulating-Splines", parameters: [Variable("Spline")], qubits: [Fixed(1)] }, instructions: [Gate(Gate { name: "Y", parameters: [], qubits: [Fixed(1)], modifiers: [] })] },
])
# ---
# name: TestProgram.test_calibrations.1
list([
MeasureCalibrationDefinition { qubit: Some(Fixed(2)), parameter: "theta", instructions: [Gate(Gate { name: "Z", parameters: [], qubits: [Fixed(2)], modifiers: [] })] },
MeasureCalibrationDefinition { identifier: MeasureCalibrationIdentifier { qubit: Some(Fixed(2)), parameter: "theta[0]" }, instructions: [Gate(Gate { name: "Z", parameters: [], qubits: [Fixed(2)], modifiers: [] })] },
])
# ---
# name: TestProgram.test_frames
Expand Down Expand Up @@ -98,8 +98,8 @@
# name: test_def_gate
'''
DEFGATE SQRT-X AS MATRIX:
0.5+0.5i, 0.5-0.5i
0.5-0.5i, 0.5+0.5i
0.5+0.5i, 0.5-0.5i
0.5-0.5i, 0.5+0.5i

SQRT-X 0

Expand All @@ -108,8 +108,8 @@
# name: test_def_gate_with_parameters
'''
DEFGATE MY_RX(%theta) AS MATRIX:
cos(%theta/2), -1.0i*sin(%theta/2)
-1.0i*sin(%theta/2), cos(%theta/2)
cos(%theta/2), -1.0i*sin(%theta/2)
-1.0i*sin(%theta/2), cos(%theta/2)


'''
Expand All @@ -135,8 +135,8 @@
# name: test_defgate
'''
DEFGATE TEST AS MATRIX:
1, 0
0, 1
1, 0
0, 1

'''
# ---
Expand All @@ -146,11 +146,19 @@
# name: test_defgate.2
'TEST 1 2'
# ---
# name: test_defgate_integer_input
'''
DEFGATE TEST AS MATRIX:
1, 0
0, 1

'''
# ---
# name: test_defgate_param
'''
DEFGATE TEST AS MATRIX:
1, 0
0, 1
1, 0
0, 1

'''
# ---
Expand Down Expand Up @@ -265,8 +273,8 @@
# name: test_merge_with_pauli_noise
'''
DEFGATE pauli_noise AS MATRIX:
1, 0
0, 1
1, 0
0, 1

PRAGMA ADD-KRAUS pauli_noise 0 "(0.0 0.0 0.0 0.0)"
PRAGMA ADD-KRAUS pauli_noise 0 "(0.0 1.0 1.0 0.0)"
Expand All @@ -282,10 +290,10 @@
# name: test_multiqubit_gate
'''
DEFGATE X-SQRT-X AS MATRIX:
0, 0.5+0.5i, 0, 0.5-0.5i
0.5+0.5i, 0, 0.5-0.5i, 0
0, 0.5-0.5i, 0, 0.5+0.5i
0.5-0.5i, 0, 0.5+0.5i, 0
0, 0.5+0.5i, 0, 0.5-0.5i
0.5+0.5i, 0, 0.5-0.5i, 0
0, 0.5-0.5i, 0, 0.5+0.5i
0.5-0.5i, 0, 0.5+0.5i, 0

X-SQRT-X 0 1

Expand All @@ -294,13 +302,13 @@
# name: test_pauli_gate
'''
DEFGATE U(%beta) p q AS PAULI-SUM:
ZZ((-1*%beta)/4) p q
Z(%beta/4) p
Z(%beta/4) q
ZZ((-1*%beta)/4) p q
Z(%beta/4) p
Z(%beta/4) q

DEFGATE V AS MATRIX:
1, 0
0, 1
1, 0
0, 1

U(2) 1 0

Expand Down Expand Up @@ -336,8 +344,8 @@
# name: test_prog_merge
'''
DEFGATE test AS MATRIX:
1, 0
0, 1
1, 0
0, 1

X 0
test 0
Expand All @@ -349,11 +357,11 @@
# name: test_prog_merge.1
'''
DEFGATE test AS MATRIX:
1, 0
0, 1
1, 0
0, 1

DEFGATE PERM AS PERMUTATION:
0, 1, 3, 2
0, 1, 3, 2

X 0
test 0
Expand Down
Loading

0 comments on commit 7026c25

Please sign in to comment.