<commit_before>import base64
import json
import struct
import time

from six import integer_types
import numpy as np

from pyquil.wavefunction import Wavefunction

OCTETS_PER_DOUBLE_FLOAT = 8
OCTETS_PER_COMPLEX_DOUBLE = 2 * OCTETS_PER_DOUBLE_FLOAT


def wait_for_job(res, ping_time=0.5):
    """
    Blocks execution and waits for an async Forest Job to complete.

    :param JobResult res: The JobResult object to wait for.
    :param ping_time: The interval (in seconds) at which to ping the server.
    :return: The completed JobResult
    """
    while not res.is_done():
        time.sleep(ping_time)
    return res


class JobResult(object):
    def __init__(self, qpu, success, result=None, payload=None):
        """
        :param QPUConnection qpu:
        :param bool success:
        :param dict result: JSON dictionary of the result message
        """
        self.qpu = qpu
        self.success = success
        self.result = result
        self.payload = payload

    def is_done(self):
        """
        Polls Forest API for status, then returns updated status.

        :return: Returns True if the Job is completed and False otherwise.
        :rtype: bool
        """
        self.get()
        return 'result' in self.result

    def job_id(self):
        """
        :return: Returns the id of this job
        :rtype: str
        """
        return self.result['jobId']

    def get(self):
        """
        Gets an update from the Forest API on the status of this job.
        :return: The JobResult object with fields updated.
        :rtype: JobResult
        """
        return self.qpu.get_job(self)

    def _update(self, success, result):
        self.success = success
        self.result = result
        return self

    def decode(self):
        """
        Decodes the result of the job.
        :return: Depends on the type of job. A JSON object.
        """
        return json.loads(self.result['result'])

    def __str__(self):
        return str(self.result)

    @classmethod
    def load_res(cls, qpu, response):
        result = response.json()
        return cls(qpu, response.ok, result)


class WavefunctionResult(JobResult):

    def decode(self):
        string_result = self.result['result']
        coef_string = base64.b64decode(string_result.encode("utf-8"))
        wvf, classical_mem = recover_complexes(coef_string=coef_string,<commit_msg>fixes bug in job result decoding (#131)<commit_after>import base64
import json
import struct
import time

from six import integer_types
import numpy as np

from pyquil.wavefunction import Wavefunction

OCTETS_PER_DOUBLE_FLOAT = 8
OCTETS_PER_COMPLEX_DOUBLE = 2 * OCTETS_PER_DOUBLE_FLOAT


def wait_for_job(res, ping_time=0.5):
    """
    Blocks execution and waits for an async Forest Job to complete.

    :param JobResult res: The JobResult object to wait for.
    :param ping_time: The interval (in seconds) at which to ping the server.
    :return: The completed JobResult
    """
    while not res.is_done():
        time.sleep(ping_time)
    return res


class JobResult(object):
    def __init__(self, qpu, success, result=None, payload=None):
        """
        :param QPUConnection qpu:
        :param bool success:
        :param dict result: JSON dictionary of the result message
        """
        self.qpu = qpu
        self.success = success
        self.result = result
        self.payload = payload

    def is_done(self):
        """
        Polls Forest API for status, then returns updated status.

        :return: Returns True if the Job is completed and False otherwise.
        :rtype: bool
        """
        self.get()
        return 'result' in self.result

    def job_id(self):
        """
        :return: Returns the id of this job
        :rtype: str
        """
        return self.result['jobId']

    def get(self):
        """
        Gets an update from the Forest API on the status of this job.
        :return: The JobResult object with fields updated.
        :rtype: JobResult
        """
        return self.qpu.get_job(self)

    def _update(self, success, result):
        self.success = success
        self.result = result
        return self

    def decode(self):
        """
        Decodes the result of the job.
        :return: Depends on the type of job. A JSON object.
        """
        return self.result['result']

    def __str__(self):
        return str(self.result)

    @classmethod
    def load_res(cls, qpu, response):
        result = response.json()
        return cls(qpu, response.ok, result)


class WavefunctionResult(JobResult):

    def decode(self):
        string_result = self.result['result']
        coef_string = base64.b64decode(string_result.encode("utf-8"))
        wvf, classical_mem = recover_complexes(coef_string=coef_string,
{'fraction_non_alphanumeric': 0.08599916909015372, 'fraction_numerical': 0.00519318653926049, 'mean_word_length': 2.3765778401122017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6162011', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1369, 'n_words': 455}