Skip to content

Commit

Permalink
Foundational support for implementing Program with qcs-sdk-python (#1518
Browse files Browse the repository at this point in the history
)

* tear out members, start replacing with rust API (WIP)

* first pass as integrating with qcs_sdk.quil

* fix low hanging fruit, introduce snapshot testing for passing tests,
annotate others with improvements that need to be made

* update poetry.lock

* more test annotations, fixes, snapshots

* deprecation warnings and cleanup

* Gate tests and snapshots, pre-replacement with quil-rs

* add test for FORKED gate

* back Gate with RSGate

* fix deprecated notice

* test improvements

* more cleanup

* various cleanups

* add more snaps

* clean up program per feedback

* feat!: The `calibrations` method on `Program` now only returns `DefCalibration`s. A `Program`s the `DefMeasureCalibrations` can be retrieved via the new `measure_calibrations` method.

* update instruction handling logic

* deprecate valid protoquil/quilt methods

* add compatibility layer by overriding quil_rs.Gate superclass methods

* remove gate __init__ method

* remove old comments

* revert defcal changes

* safer type checking on conversion methods

* simplify ParameterDesignator Type

* forbidden metaclass shenanigans

* Update pyquil/quil.py

Co-authored-by: Kalan <22137047+kalzoo@users.noreply.github.com>

* use deprecated decorator

* update qcs-sdk-python dependency spec

---------

Co-authored-by: Kalan <22137047+kalzoo@users.noreply.github.com>
  • Loading branch information
MarquessV and kalzoo committed Mar 7, 2023
1 parent 33a5e2c commit 9c7de67
Show file tree
Hide file tree
Showing 25 changed files with 2,191 additions and 1,144 deletions.
1,344 changes: 1,116 additions & 228 deletions poetry.lock

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pyquil"
version = "4.0.0-rc.6"
version = "4.0.0-rc.4"
description = "A Python library for creating Quantum Instruction Language (Quil) programs."
authors = ["Rigetti Computing <softapps@rigetti.com>"]
readme = "README.md"
Expand All @@ -19,14 +19,14 @@ keywords = ["quantum", "quil", "programming", "hybrid"]
packages = [{ include = "pyquil" }]

[tool.poetry.dependencies]
python = "^3.7,<3.11"
python = "^3.8,<3.11"
numpy = "^1.21"
scipy = "^1.7.3"
lark = "^0.11.1"
rpcq = "^3.10.0"
networkx = "^2.5"
importlib-metadata = { version = ">=3.7.3,<5", python = "<3.8" }
qcs-sdk-python = "0.4.2"
qcs-sdk-python = { git = "https://github.com/rigetti/qcs-sdk-rust", subdirectory = "crates/python", branch = "reexport-quil-py" }
qcs-api-client = ">=0.21.0,<0.22.0"
retry = "^0.9.2"
types-python-dateutil = "^2.8.19"
Expand All @@ -40,6 +40,7 @@ Sphinx = { version = "^4.0.2", optional = true }
sphinx-rtd-theme = { version = "^0.5.2", optional = true }
nbsphinx = { version = "^0.8.6", optional = true }
recommonmark = { version = "^0.7.1", optional = true }
deprecation = "^2.1.0"

[tool.poetry.dev-dependencies]
black = "^22.8.0"
Expand All @@ -55,6 +56,7 @@ pytest-freezegun = "^0.4.2"
respx = "^0.20"
nest-asyncio = "^1.5.6"
mock = { version = "^4.0", python = "<3.8" }
syrupy = "^3.0.6"

[tool.poetry.extras]
latex = ["ipython"]
Expand Down
21 changes: 5 additions & 16 deletions pyquil/api/_abstract_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,19 @@ def quil_to_native_quil(self, program: Program, *, protoquil: Optional[bool] = N
"""
Convert a Quil program into native Quil, which is supported for execution on a QPU.
"""

# This is a work-around needed because calling `qcs_sdk.compile` happens _before_
# the event loop is available. Wrapping it in a Python async function ensures that
# the event loop is available. This is a limitation of pyo3:
# https://pyo3.rs/v0.17.1/ecosystem/async-await.html#a-note-about-asynciorun
async def _compile(*args, **kwargs) -> str: # type: ignore
return await qcs_sdk.compile(*args, **kwargs)

# TODO This ISA isn't always going to be available. Specifically, if the quantum processor is
# a QVM-type processor, then `quantum_processor` will have a CompilerISA, not a QCSISA.
# This will have to be addressed as part of this issue: https://github.com/rigetti/pyquil/issues/1496
target_device = compiler_isa_to_target_quantum_processor(self.quantum_processor.to_compiler_isa())
native_quil = self._event_loop.run_until_complete(
_compile(
program.out(calibrations=False),
json.dumps(target_device.asdict(), indent=2), # type: ignore
timeout=self._compiler_client.timeout,
)

native_quil = qcs_sdk.compile(
program.out(calibrations=False),
json.dumps(target_device.asdict(), indent=2), # type: ignore
timeout=self._compiler_client.timeout,
)

native_program = Program(native_quil)
native_program.num_shots = program.num_shots
native_program._calibrations = program._calibrations
native_program._waveforms = program._waveforms
native_program._memory = program._memory.copy()

return native_program
Expand Down
17 changes: 4 additions & 13 deletions pyquil/api/_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,10 @@ def native_quil_to_executable(self, nq_program: Program) -> QuantumExecutable:
"""
rewrite_response = qcs_sdk.rewrite_arithmetic(nq_program.out())

# This is a work-around needed because calling `qcs_sdk.translate` happens _before_
# the event loop is available. Wrapping it in a Python async function ensures that
# the event loop is available. This is a limitation of pyo3:
# https://pyo3.rs/v0.17.1/ecosystem/async-await.html#a-note-about-asynciorun
async def _translate(*args):
return await qcs_sdk.translate(*args)

translated_program = self._event_loop.run_until_complete(
_translate(
rewrite_response["program"],
nq_program.num_shots,
self.quantum_processor_id,
)
translated_program = qcs_sdk.translate(
rewrite_response["program"],
nq_program.num_shots,
self.quantum_processor_id,
)

return EncryptedProgram(
Expand Down
5 changes: 1 addition & 4 deletions pyquil/api/_compiler_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,7 @@ def get_version(self) -> str:
Get version info for compiler server.
"""

async def _get_quilc_version() -> str:
return await qcs_sdk.get_quilc_version()

return self._event_loop.run_until_complete(_get_quilc_version())
return qcs_sdk.get_quilc_version()

def compile_to_native_quil(self, request: CompileToNativeQuilRequest) -> CompileToNativeQuilResponse:
"""
Expand Down
Loading

0 comments on commit 9c7de67

Please sign in to comment.