Skip to content

Commit

Permalink
Fix: allow np.ndarray in write_memory and disallow non-int and non-fl… (
Browse files Browse the repository at this point in the history
#1365)

* Fix: allow np.ndarray in write_memory and disallow non-int and non-float types

* Fix: address review comms

* Fix: match not matches in pytest.raises

* Fix: reduce test match string

* Fix: test expects pi, not 1.0
  • Loading branch information
notmgsk authored Jul 30, 2021
1 parent 6d8e1df commit fba1f30
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Changelog

### Bugfixes

- Allow `np.ndarray` when writing QAM memory. Disallow non-integer and non-float types.

[v3.0.0](https://github.com/rigetti/pyquil/releases/tag/v3.0.0)
------------------------------------------------------------------------------------

Expand Down
8 changes: 7 additions & 1 deletion pyquil/_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,20 @@ def _write_value(
if isinstance(parameter, str):
parameter = ParameterAref(name=parameter, index=0)

import numpy as np

if isinstance(value, (int, float)):
self.values[parameter] = value
elif isinstance(value, Sequence):
elif isinstance(value, (Sequence, np.ndarray)):
if parameter.index != 0:
raise ValueError("Parameter may not have a non-zero index when its value is a sequence")

for index, v in enumerate(value):
if not isinstance(v, (int, float)):
raise TypeError(f"Parameter must be numeric, not {type(value)}")
aref = ParameterAref(name=parameter.name, index=index)
self.values[aref] = v
else:
raise TypeError(f"Parameter must be numeric or an iterable of numeric values, not {type(value)}")

return self
24 changes: 22 additions & 2 deletions test/unit/test_quantum_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,8 @@ def test_qc_error(client_configuration: QCSClientConfiguration):
get_qc("5q", as_qvm=False, client_configuration=client_configuration)


def test_run_with_parameters(client_configuration: QCSClientConfiguration):
@pytest.mark.parametrize("param", [np.pi, [np.pi], np.array([np.pi])])
def test_run_with_parameters(client_configuration: QCSClientConfiguration, param):
quantum_processor = NxQuantumProcessor(nx.complete_graph(3))
qc = QuantumComputer(
name="testy!",
Expand All @@ -470,13 +471,32 @@ def test_run_with_parameters(client_configuration: QCSClientConfiguration):
MEASURE(0, MemoryReference("ro")),
).wrap_in_numshots_loop(1000)

executable.write_memory(region_name="theta", value=np.pi)
executable.write_memory(region_name="theta", value=param)
bitstrings = qc.run(executable).readout_data.get('ro')

assert bitstrings.shape == (1000, 1)
assert all([bit == 1 for bit in bitstrings])


@pytest.mark.parametrize("param", [1j, "not_a_number", ["not_a_number"]])
def test_run_with_bad_parameters(client_configuration: QCSClientConfiguration, param):
quantum_processor = NxQuantumProcessor(nx.complete_graph(3))
qc = QuantumComputer(
name="testy!",
qam=QVM(client_configuration=client_configuration),
compiler=DummyCompiler(quantum_processor=quantum_processor, client_configuration=client_configuration),
)
executable = Program(
Declare(name="theta", memory_type="REAL"),
Declare(name="ro", memory_type="BIT"),
RX(MemoryReference("theta"), 0),
MEASURE(0, MemoryReference("ro")),
).wrap_in_numshots_loop(1000)

with pytest.raises(TypeError, match=r"Parameter must be"):
executable.write_memory(region_name="theta", value=param)


def test_reset(client_configuration: QCSClientConfiguration):
quantum_processor = NxQuantumProcessor(nx.complete_graph(3))
qc = QuantumComputer(
Expand Down

0 comments on commit fba1f30

Please sign in to comment.