def test_run_and_measure_async(forest: ForestConnection):
    # The forest fixture (argument) to this test is to ensure this is
    # skipped when a forest web api key is unavailable. You could also
    # pass it to the constructor of WavefunctionSimulator() but it is not
    # necessary.
    wfnsim = WavefunctionSimulator()
    bell = Program(
        H(0),
        CNOT(0, 1),
    )
    job_id = wfnsim.run_and_measure_async(bell, trials=1000)
    assert isinstance(job_id, str)
    bitstrings = wfnsim.wait_for_job(job_id).result()
    assert bitstrings.shape == (1000, 2)
    parity = np.sum(bitstrings, axis=1) % 2
    assert np.all(parity == 0)
def test_wavefunction_async(forest: ForestConnection):
    # The forest fixture (argument) to this test is to ensure this is
    # skipped when a forest web api key is unavailable. You could also
    # pass it to the constructor of WavefunctionSimulator() but it is not
    # necessary.
    wfnsim = WavefunctionSimulator()
    bell = Program(
        H(0),
        CNOT(0, 1),
    )
    job_id = wfnsim.wavefunction_async(bell)
    assert isinstance(job_id, str)
    wfn = wfnsim.wait_for_job(job_id).result()
    np.testing.assert_allclose(wfn.amplitudes,
                               1 / np.sqrt(2) * np.array([1, 0, 0, 1]))
    np.testing.assert_allclose(wfn.probabilities(), [0.5, 0, 0, 0.5])
    assert wfn.pretty_print() == "(0.71+0j)|00> + (0.71+0j)|11>"

    bitstrings = wfn.sample_bitstrings(1000)
    parity = np.sum(bitstrings, axis=1) % 2
    assert np.all(parity == 0)