Skip to content

Commit

Permalink
Efficient unpacking of bitstring (#1276)
Browse files Browse the repository at this point in the history
h/t @mhodson-rigetti

Fix linting.

Efficient unpacking of bitstring

h/t @mhodson-rigetti
  • Loading branch information
notmgsk authored Nov 23, 2020
1 parent 918f84c commit 2ceb0ac
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Changelog

### Improvements and Changes

- Unpacking bitstrings is significantly faster (@mhodson-rigetti, @notmgsk, #1276).

### Bugfixes


Expand Down
16 changes: 3 additions & 13 deletions pyquil/wavefunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
Module containing the Wavefunction object and methods for working with wavefunctions.
"""
import itertools
import struct
import warnings
from typing import Dict, Iterator, List, Optional, Sequence, cast

Expand Down Expand Up @@ -81,18 +80,9 @@ def from_bit_packed_string(coef_string: bytes) -> "Wavefunction":
From a bit packed string, unpacks to get the wavefunction
:param coef_string:
"""
num_octets = len(coef_string)

# Parse the wavefunction
wf = np.zeros(num_octets // OCTETS_PER_COMPLEX_DOUBLE, dtype=np.cfloat)
for i, p in enumerate(range(0, num_octets, OCTETS_PER_COMPLEX_DOUBLE)):
re_be = coef_string[p : p + OCTETS_PER_DOUBLE_FLOAT]
im_be = coef_string[p + OCTETS_PER_DOUBLE_FLOAT : p + OCTETS_PER_COMPLEX_DOUBLE]
re = struct.unpack(">d", re_be)[0]
im = struct.unpack(">d", im_be)[0]
wf[i] = complex(re, im)

return Wavefunction(wf)
num_cfloat = len(coef_string) // OCTETS_PER_COMPLEX_DOUBLE
amplitude_vector = np.ndarray(shape=(num_cfloat,), buffer=coef_string, dtype=">c16")
return Wavefunction(amplitude_vector)

def __len__(self) -> int:
return len(self.amplitudes).bit_length() - 1
Expand Down

0 comments on commit 2ceb0ac

Please sign in to comment.