Exercise H.6.1(b)

(b) Complete the code below to apply the sum X+Z to the state |0> (on the main register). You can invoke add_two_unitaries(U, V) from the last challenge, and access the matrix form of the Paulis using, e.g., qml.PauliX.matrix .

@qml.qnode(dev)
def X_plus_Z():
    """Apply X + Z to |0> and return the state."""
    ##################
    # YOUR CODE HERE #
    ##################
    U = qml.PauliX.matrix
    V = qml.PauliZ.matrix
    add_two_unitaries(U,V)
    return qml.state()

print("The amplitudes on the main register are proportional to", X_plus_Z()[:2], ".")

Error: ‘function’ object has no attribute ‘wires’

Where exactly do I need to add the wires, I just invoked the add_two_unitaries from the previous code, which was correct as it was accepted.

Where I’m getting it wrong?

Hey @Monit_Sharma! Welcome to the forum :rocket:!

The .matrix method isn’t static, meaning that the object has to actually be created and then .matrix can be called:

    U = qml.PauliX(0).matrix()
    V = qml.PauliZ(0).matrix()

Be careful, though! Because the method isn’t static, if you do this within a QNode those operators will be queued! Here’s an example where doing this (might) unintentionally puts those operations in a circuit:

dev = qml.device("default.qubit", wires = 1)

@qml.qnode(dev)
def circuit():
    U = qml.PauliX(0).matrix()
    V = qml.PauliZ(0).matrix()
    return qml.state()

print(qml.draw(circuit)())

'''
0: ──X──Z─┤  State
'''

I recommend that you use compute_matrix() instead — it is a static method, so the operator doesn’t need to be created and, therefore, you don’t have this unintentional behaviour.

    U = qml.PauliX.compute_matrix()
    V = qml.PauliZ.compute_matrix()