Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Changelog

- Allow `np.ndarray` when writing QAM memory. Disallow non-integer and non-float types.
- Fix typo where `qc.compiler.calibration_program` should be `qc.compiler.get_calibration_program()`.
- `DefFrame` string-valued fields that contain JSON strings now round trip to valid Quil and back to
JSON via `DefFrame.out` and `parse`. Quil and JSON both claim `"` as their only string delimiter,
so the JSON `"`s are escaped in the Quil.

[v3.0.0](https://github.com/rigetti/pyquil/releases/tag/v3.0.0)
------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pyquil/_parser/grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def_circuit : "DEFCIRCUIT" name [ variables ] [ qubit_designators ] ":" indented
// | "DEFCIRCUIT" name [ variables ] qubit_designators ":" indented_instrs -> def_circuit_qubits

def_frame : "DEFFRAME" frame ( ":" frame_spec+ )?
frame_spec : _NEWLINE_TAB frame_attr ":" (expression | string )
frame_spec : _NEWLINE_TAB frame_attr ":" ( expression | string )
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, so it already was string. I must've been looking at the wrong/old branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, we were looking at master, but this was already done on rc. When I got here and saw it I assumed you'd done it. :)

!frame_attr : "SAMPLE-RATE"
| "INITIAL-FREQUENCY"
| "DIRECTION"
Expand Down
6 changes: 2 additions & 4 deletions pyquil/_parser/parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import pkgutil
import operator
from typing import List
Expand Down Expand Up @@ -142,10 +143,7 @@ def def_frame(self, frame, *specs):
for (spec_name, spec_value) in specs:
name = names.get(spec_name, None)
if name:
if isinstance(spec_value, str) and spec_value[0] == '"' and spec_value[-1] == '"':
# Strip quotes if necessary
spec_value = spec_value[1:-1]
options[name] = spec_value
options[name] = json.loads(str(spec_value))
else:
raise ValueError(
f"Unexpectected attribute {spec_name} in definition of frame {frame}. " f"{frame}, {specs}"
Expand Down
7 changes: 4 additions & 3 deletions pyquil/quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
Contains the core pyQuil objects that correspond to Quil instructions.
"""
import collections
import json

from numbers import Complex
from typing import (
Any,
Expand Down Expand Up @@ -1375,7 +1377,6 @@ def out(self) -> str:
for value, name in options:
if value is None:
continue
if isinstance(value, str):
value = f'"{value}"'
r += f"\n {name}: {value}"
else:
r += f"\n {name}: {json.dumps(value)}"
return r + "\n"
23 changes: 23 additions & 0 deletions test/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,29 @@ def test_parsing_defframe():
assert excp.value.token == Token("IDENTIFIER", "UNSUPPORTED")


def test_parsing_defframe_round_trip_with_json():
fdef = DefFrame(
frame=Frame(qubits=[FormalArgument("My-Cool-Qubit")], name="bananas"),
direction="go west",
initial_frequency=123.4,
center_frequency=124.5,
hardware_object='{"key1": 3.1, "key2": "value2"}',
sample_rate=5,
)
fdef_out = fdef.out()
assert (
fdef.out()
== r"""DEFFRAME My-Cool-Qubit "bananas":
DIRECTION: "go west"
Copy link
Contributor

Choose a reason for hiding this comment

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

nice

INITIAL-FREQUENCY: 123.4
CENTER-FREQUENCY: 124.5
HARDWARE-OBJECT: "{\"key1\": 3.1, \"key2\": \"value2\"}"
SAMPLE-RATE: 5
"""
)
parse_equals(fdef_out, fdef)


def test_parsing_defcal():
parse_equals("DEFCAL X 0:\n" " NOP\n", DefCalibration("X", [], [Qubit(0)], [NOP]))
parse_equals(
Expand Down