Multi-qubit systems I.11.1

I am trying to solve the first exercise but it looks like I am having an issue implementing the right state. There is my code:

num_wires = 3
dev = qml.device('default.qubit', wires=num_wires)

@qml.qnode(dev)
def make_basis_state(basis_id):
    """Produce the 3-qubit basis state corresponding to |basis_id>.
    
    Note that the system starts in |000>.

    Args:
        basis_id (int): An integer value identifying the basis state to construct.
        
    Returns:
        array[complex]: The computational basis state |basis_id>.
    """

    ##################
    lis=[i for i in range(-basis_id, basis_id+1)]
    lis_1 = []
    
    for i in lis:
        a=np.binary_repr(i,width=3)
        lis_1.append(a)
    list_2 = []
    for j in range(len(lis_1)):
        string_list= list(lis_1[j])
        list_2.append(list(map(int, string_list)))
    #del list_2[3]
    for k in list_2:    
        qml.BasisEmbedding(np.array(k), wires=range(basis_id))
    #quantum_state = circuit(data_list)

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

    # CREATE THE BASIS STATE
    
    return qml.state()


basis_id = 3
print(f"Output state = {make_basis_state(basis_id)}")

And the error that I get is:

Error: Features must be of length 0; got length 3 (features=[0 0 0]).

And, finally, the output is:

Output state = [0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j]

I would be delighted to receive y’all propositions on how to solve it.

Hi @baudoin ,

As you noticed qml.BasisEmbedding can help you here. However the solution is much simpler than you thought! Take a look at the documentation for qml.BasisEmbedding. What if you run the example shown there but instead of using X = [1,1,1] you use X=7? It means that the features don’t necessarily need to be in binary already. This will make your life much easier!

1 Like