# Codebook: I.1 All about qubits: Problem 3

**URL:** <https://discuss.pennylane.ai/t/codebook-i-1-all-about-qubits-problem-3/1616>\
**Category:** Codebook\
**Created:** [January 18, 2022, 2:22pm UTC](https://discuss.pennylane.ai/t/codebook-i-1-all-about-qubits-problem-3/1616 "2022-01-18T14:22:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimonPRichards](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@SimonPRichards](https://discuss.pennylane.ai/u/SimonPRichards)\
**Post date:** [January 18, 2022, 2:22pm UTC](https://discuss.pennylane.ai/t/codebook-i-1-all-about-qubits-problem-3/1616/1 "2022-01-18T14:22:01Z")

</div>

I have just started working my way through the Codebook.

The following code is being rejected with the error message shown. However, it executes as expected in Visual Studio Code.

I would be hugely grateful of someone could point out my error as it is not possible to progress in the codebook without rectifying the error.

{START}  
def measure\_state(state, num\_meas):  
“”"Simulate a quantum measurement process.

```
Args:
    state (array[complex]): A normalized qubit state vector. 
    num_meas (int): The number of measurements to take
    
Returns:
    array[int]: A set of num_meas samples, 0 or 1, chosen according to the probability 
    distribution defined by the input state.
"""

##################
# YOUR CODE HERE #
##################
Results = np.empty(num_meas, dtype=int)

# COMPUTE THE MEASUREMENT OUTCOME PROBABILITIES
Ket0Probability = np.real(state[0]*(state[0].conjugate())

# RETURN A LIST OF SAMPLE MEASUREMENT OUTCOMES
Results = np.random.choice(2, num_meas, p=[Ket0Probability, (1-Ket0Probability)])

return Results

```

state = np.array([0.8, 0.6])  
num\_meas = 10

print(measure\_state(state,num\_meas))  
{END}

The error is “Error: invalid syntax (, line 20)”

Line 20 is the call to np.random.choice

I have tried Line 20 with “2” replaced with [0,1].

I have tried Line 20 dropping “p=”.

All four variants work in Visual Studio Code.

Thanks in advance,

Simon

---

<div class="post-metadata">

**Author:** ![adusko](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/adusko/32/883_2.png) [@adusko](https://discuss.pennylane.ai/u/adusko)\
**Post date:** [January 18, 2022, 3:55pm UTC](https://discuss.pennylane.ai/t/codebook-i-1-all-about-qubits-problem-3/1616/2 "2022-01-18T15:55:58Z")

</div>

Hi @SimonPRichards,  
Welcome to the Xanadu community!  
I think the invalid syntax refers to missing a “)” below.

> [@SimonPRichards](#):
>
> np.real(state[0]\*(state[0].conjugate())

Please try again with:  
np.real(state[0]\*(state[0].conjugate())**)**  
Let me know if it worked.

---

<div class="post-metadata">

**Author:** ![SimonPRichards](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@SimonPRichards](https://discuss.pennylane.ai/u/SimonPRichards)\
**Post date:** [January 18, 2022, 4:40pm UTC](https://discuss.pennylane.ai/t/codebook-i-1-all-about-qubits-problem-3/1616/3 "2022-01-18T16:40:28Z")

</div>

Thank you so much - that was it!

I cant recall whether at some point I had the same error in Visual Studio Code and fixed it, but I was thrown as the incorrect line you pointed out is Line 18 and the error refers to Line 20.

I would never have found it.

Thanks again,

Simon

---

<div class="post-metadata">

**Author:** ![adusko](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/adusko/32/883_2.png) [@adusko](https://discuss.pennylane.ai/u/adusko)\
**Post date:** [January 18, 2022, 6:54pm UTC](https://discuss.pennylane.ai/t/codebook-i-1-all-about-qubits-problem-3/1616/4 "2022-01-18T18:54:16Z")

</div>

Hi @SimonPRichards,  
I’m happy to have helped!  
If you have any other question just let us know!
