codercise A.4.1

Hello, my code in codercise A.4.1 seems correct to me but it shows the following error:

Input unit must be of format (16, 16) or (batch_size, 16, 16) to act on 4 threads.

Can anyone identify what is wrong?

Função para criar a matriz do oráculo

def multisol_oracle_matrix(combos):

# Para cada combinação na lista de combinações
indices = [np.ravel_multi_index(combo, [2] * len(combo)) for combo in combos]

# Cria a matriz identidade de tamanho 2^n, onde n é o número de bits
my_array = np.identity(2**n_bits)

# Modifica a entrada da diagonal correspondente ao índice da solução
my_array[indices, indices] = [(-1) for combo in combos]
    
return my_array

Circuito quântico para testar um par de combinações

@qml.qnode(dev)
def multisol_pair_circuit(x_tilde, combos):

# Inicializar x_tilde
for i in range(n_bits - 1):
    if x_tilde[i] == 1:
        qml.PauliX(wires=i)

# Aplica a porta Hadamard ao último qubit
qml.Hadamard(wires=n_bits-1)

# Aplicar o oráculo
oracle_matrix = multisol_oracle_matrix(combos)
qml.QubitUnitary(oracle_matrix, wires=range(n_bits))

# Aplica outra porta Hadamard ao último qubit
qml.Hadamard(wires=n_bits-1)

# Retorna as probabilidades no último qubit
return qml.probs(wires=n_bits-1)

Hey @robsonpaulinos,

For this particular exercise our goal is to create an Oracle for all the secret combinations to the lock. If you remember in “Magic 8 ball” when we created the Oracle, we only changed the entry to “-1” for the the secret combination and np.ravel_multi_index returns an index value where the specified entry is in the array. So for example, lets say our array = [8,6] , combo=[6] and we run np.ravel_multi_index(combo,[2] x len(combo)), it will return 1, since “6” is at the first index in our array.

So now the goal would be to change the diagonal entry in our Oracle at the first row to “-1”.
But for this particular exercise, we have multiple secret combinations. Thus, “indices = [np.ravel_multi_index(combo, [2] * len(combo)) for combo in combos]” return a list of indexes.

In your code where you wrote “my_array[indices, indices] = [(-1) for combo in combos]” doesn’t make any sense because indices is a list and when you write “my_array[indices,indices]”, it gives you an error.

Hope this explanation helps you :slight_smile: .

2 Likes

Thank you very much for your explanation @Krishna_Bhatia.

I found the error after I posted the question. I had not removed the “pass” that is found after the “your code here” causing the function to not return the array :unamused: After doing this, it worked correctly.

###################

YOUR CODE HERE

##################
pass

1 Like

:joy: I’ve been there too @robsonpaulinos. I forgot to delete that “pass” at least four times now and wasted too many hours on it!!!

2 Likes