# Buggy code
def meyer_penny_program():
    """
    Returns the program to simulate the Meyer-Penny Game
    The full description is available in docs/source/examples.rst

    :return: pyQuil Program
    """

    picard_register = 1
    answer_register = 0

    then_branch = pq.Program(X(0))
    else_branch = pq.Program(I(0))

    return (
        pq.Program()
        # Prepare Qubits in Heads state or superposition, respectively
        .inst(X(0), H(1))
        # Q puts the coin into a superposition
        .inst(H(0))
        # Picard makes a decision and acts accordingly
        .measure(1, picard_register)
        .if_then(picard_register, then_branch, else_branch)
        # Q undoes his superposition operation
        .inst(H(0))
        # The outcome is recorded into the answer register
        .measure(0, answer_register)
    )


# 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 meyer_penny_program():
    """
    Returns the program to simulate the Meyer-Penny Game
    The full description is available in docs/source/examples.rst

    :return: pyQuil Program
    """
    prog = pq.Program()
    ro = prog.declare("ro")
    picard_register = ro[1]
    answer_register = ro[0]

    then_branch = pq.Program(X(0))
    else_branch = pq.Program(I(0))

    # Prepare Qubits in Heads state or superposition, respectively
    prog.inst(X(0), H(1))
    # Q puts the coin into a superposition
    prog.inst(H(0))
    # Picard makes a decision and acts accordingly
    prog.measure(1, picard_register)
    prog.if_then(picard_register, then_branch, else_branch)
    # Q undoes his superposition operation
    prog.inst(H(0))
    # The outcome is recorded into the answer register
    prog.measure(0, answer_register)

    return prog