# Buggy code
def run_bell_low_level(n_shots=1000):
    # Step 1. Get some device components
    qc = get_qc("9q-generic-qvm")
    compiler = qc.compiler
    qam = qc.qam
    del qc

    q = [4, 5]  # qubits

    # Step 2. Construct your program
    program = Program()
    program += H(q[0])
    program += CNOT(q[0], q[1])

    # Step 2.1. Manage read-out memory
    ro = program.declare("ro", memory_type="BIT", memory_size="2")
    program += MEASURE(q[0], ro[0])
    program += MEASURE(q[1], ro[1])

    # Step 2.2. Run the program in a loop
    program = program.wrap_in_numshots_loop(n_shots)

    # Step 3. Compile and run
    nq_program = compiler.quil_to_native_quil(program)
    executable = compiler.native_quil_to_executable(nq_program)
    bitstrings = (
        qam.load(executable)
        .run()
        .wait()
        .read_from_memory_region(region_name="ro", offsets=True)
    )

    # Bincount bitstrings
    basis = np.array([2**i for i in range(len(q))])
    ints = np.sum(bitstrings * basis, axis=1)
    print("bincounts", np.bincount(ints))

    # Check parity
    parities = np.sum(bitstrings, axis=1) % 2
    print("avg parity", np.mean(parities))


# Traceback (ValueError)
`ValueError: if c is a list/tuple, its first member should be a string

Please enter number of sides: 60

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-30-d058bffa3dca> in <module>()
65     number_of_sides = int(input("Please enter number of sides: "))
66     while True:
---> 67         print(roll_die(number_of_sides))

<ipython-input-30-d058bffa3dca> in roll_die(n)
52     addresses = list(range(qubits_needed(n)))
53     if not n in dice:
---> 54         dice[n] = die_program(n)
55     die = dice[n]
56     # Generate results and do rejection sampling.

<ipython-input-30-d058bffa3dca> in die_program(n)
31     # Measure everything.
32     for q in range(qubits):
---> 33         prog.measure(q, [q])
34     return prog
35

~/anaconda3/lib/python3.6/site-packages/pyquil/quil.py in measure(self, qubit_index, classical_reg)
284         :rtype: Program
285         """
--> 286         return self.inst(MEASURE(qubit_index, classical_reg))
287
288     def reset(self, qubit_index=None):

~/anaconda3/lib/python3.6/site-packages/pyquil/gates.py in MEASURE(qubit, classical_reg)
489         address = MemoryReference("ro", classical_reg)
490     else:
--> 491         address = unpack_classical_reg(classical_reg)
492     return Measurement(qubit, address)
493

~/anaconda3/lib/python3.6/site-packages/pyquil/quilatom.py in unpack_classical_reg(c)
137             c = (c[0], 0)
138         if not isinstance(c[0], str):
--> 139             raise ValueError("if c is a list/tuple, its first member should be a string")
140         if not isinstance(c[1], int):
141             raise ValueError("if c is a list/tuple, its second member should be an int")

ValueError: if c is a list/tuple, its first member should be a string `

# Fixed code
def run_bell_low_level(n_shots=1000):
    # Step 1. Get some device components
    qc = get_qc("9q-generic-qvm")
    compiler = qc.compiler
    qam = qc.qam
    del qc

    q = [4, 5]  # qubits

    # Step 2. Construct your program
    program = Program()
    program += H(q[0])
    program += CNOT(q[0], q[1])

    # Step 2.1. Manage read-out memory
    ro = program.declare("ro", memory_type="BIT", memory_size="2")
    program += MEASURE(q[0], ro[0])
    program += MEASURE(q[1], ro[1])

    # Step 2.2. Run the program in a loop
    program = program.wrap_in_numshots_loop(n_shots)

    # Step 3. Compile and run
    nq_program = compiler.quil_to_native_quil(program)
    executable = compiler.native_quil_to_executable(nq_program)
    bitstrings = (
        qam.load(executable).run().wait().read_from_memory_region(region_name="ro")
    )

    # Bincount bitstrings
    basis = np.array([2**i for i in range(len(q))])
    ints = np.sum(bitstrings * basis, axis=1)
    print("bincounts", np.bincount(ints))

    # Check parity
    parities = np.sum(bitstrings, axis=1) % 2
    print("avg parity", np.mean(parities))