Codebook: The Magic 8-Ball

Ask here about the “The Magic 8-Ball” Codebook topic from the “Basic Quantum Algorithms” module.

Hi,
I completed the The Magic 8-Ball but I feel there could be a better way.

In exercise A.2.1 I used this code:

def oracle_matrix(combo):
    """Return the oracle matrix for a secret combination.
    
    Args:
        combo (list[int]): A list of bits representing a secret combination.
         
    Returns: 
        array[float]: The matrix representation of the oracle.
    """
    index = np.ravel_multi_index(combo, [2]*len(combo)) # Index of solution
    my_array = np.identity(2**len(combo)) # Create the identity matrix


    ##################
    # YOUR CODE HERE #
    ##################

    # MODIFY DIAGONAL ENTRY CORRESPONDING TO SOLUTION INDEX
    for i in combo:
       my_array[i,i] = -1    

    return my_array

The system says “correct”.
But how can this be done using index?
BTW: if I insert print(combo) nothing happens. Is there a way to print out some variables?

(yes, I know, this is a Python question and not really a QC-question)

Thanks and regards.
jomu

Hi @jomu, welcome to the Forum!

I think in this case you were lucky that your solution worked :sweat_smile: .
You’re right in the fact that you should modify a diagonal entry of my_array. However note that the instruction is to modify the entry corresponding to the solution index. This is where you use index, not combo :slightly_smiling_face:.

Your second question is totally relevant, I’d say it’s more a Codebook question than a Python question in fact!

The answer is, you should add your print statements outside of your functions. For example if you run the following code you’ll be able to print the word ‘test’. You won’t however be able to print the secret combination (combo) since it hasn’t been defined there.

def oracle_matrix(combo):
    """Return the oracle matrix for a secret combination.
    
    Args:
        combo (list[int]): A list of bits representing a secret combination.
         
    Returns: 
        array[float]: The matrix representation of the oracle.
    """
    index = np.ravel_multi_index(combo, [2]*len(combo)) # Index of solution
    my_array = np.identity(2**len(combo)) # Create the identity matrix

    ##################
    # YOUR CODE HERE #
    ##################

    # MODIFY DIAGONAL ENTRY CORRESPONDING TO SOLUTION INDEX
    

    return my_array

print('test')

I hope this helps!

Thanks a lot.
The solution is really easy (just a numpy feature).

Interesting @jomu . There’s also a solution that doesn’t require the use of any numpy features. But I’m glad you solved it! :smile:

Hi @jomu,

I guess what @CatalinaAlbornoz is trying to say in laymen terms is to ONLY modify the entry in the matrix to “-1” that is corresponding to the SECRET COMBINATION of the lock.

Right now your code is modifying all the diagonal entries in the matrix which is incorrect.
Only ONE entry in the entire matrix needs to be changed to “-1”.

Hope this helps :slight_smile: !!

Many thanks your help - i already fixed this

2 Likes

Hi @CatalinaAlbornoz I think I found a typo in Codercise A.2.1: The Matrix Form of the Oracle:

The first sentence states:

In a combination lock, we can represent the oracle as a unitary operator: $$U_f=I-2\ket{s}\bra{s}$$ where \ket{s} is the uniform superposition state. Applying the oracle is called a query.

If I am understanding correctly, this should instead read

In a combination lock, we can represent the oracle as a unitary operator: $$U_f=I-2\ket{s}\bra{s}$$ where \ket{s} is the the right n-bit combination for the lock as a basis vector. Applying the oracle to a uniform superposition state is called a query.

I believe that \ket{s} here actually refers to the

where is the uniform superposition state. Many thanks and regards

Apologies for the improper formatting. I don’t see an option to edit, so I’ll reiterate

The first sentence states:

In a combination lock, we can represent the oracle as a unitary operator: U_f=I-2\vert{\mathbf{s}}\rangle\langle{\mathbf{s}}\vert where \vert \mathbf{s}\rangle is the uniform superposition state. Applying the oracle is called a query.

If I am understanding correctly, this should instead read

In a combination lock, we can represent the oracle as a unitary operator: U_f=I-2\vert{\mathbf{s}}\rangle\langle{\mathbf{s}}\vert where \vert \mathbf{s}\rangle is the the right n-bit combination for the lock as a basis vector. Applying the oracle to a uniform superposition state is called a query.

1 Like

Hi @jhanschoo , you’re right. Thank you for this new wording suggestion!! :raised_hands: