# What is the meaning if we feed a vector into rotation gates instead of a value?

Hello, I have been confusing about the problems for a long time.

My code is illustrated as below:

import pennylane as qml
import numpy as np

qubit_num = 2
dev = qml.device("default.qubit", wires=qubit_num)
@qml.qnode(dev)
def circuit(x):
qml.RZ(x[:, 0], wires=0)
qml.RZ(x[:, 1], wires=1)
return qml.state()

x = np.random.randn(5, 2)/10
print(x)
print(circuit(x))

'''
terminal:
[[-0.00343289  0.07952211]
[-0.11131135 -0.0997912 ]
[-0.11075909 -0.22640184]
[ 0.15814072 -0.11102991]
[ 0.00028335 -0.10640625]]
0.        +0.j        ]
[0.99443463+0.10535539j 0.        +0.j         0.        +0.j
0.        +0.j        ]
[0.98582393+0.1677831j  0.        +0.j         0.        +0.j
0.        +0.j        ]
[0.99972258-0.02355323j 0.        +0.j         0.        +0.j
0.        +0.j        ]
[0.99859257+0.05303655j 0.        +0.j         0.        +0.j
0.        +0.j        ]]
'''


As you can see, the parameters of qml.RZ are 5-dim vectors.
Then the circuit returns five different states.

However, if I replace qml.state() with expectations, the circuit will return identical values:

@qml.qnode(dev)
def circuit(x):
qml.RZ(x[:, 0], wires=0)
qml.RZ(x[:, 1], wires=1)
exp_vals_z = [qml.expval(qml.PauliZ(wires=w)) for w in [0, 1]]
return exp_vals_z

x = np.random.randn(5, 2)/10
print(x)
print(circuit(x))

'''
terminal:
[[-0.09886417 -0.08193338]
[-0.07531188  0.0685733 ]
[-0.04081865  0.06245962]
[ 0.13144776 -0.09798962]
[-0.16170616  0.07845339]]
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
'''


The measurement values for each wire are identical.
Could I say that the values are identical since the values are averaged (i.e. expectation values)?

Sincerely,
Jeff

1 Like

Hi @q36111095! The reason why you are getting different state vectors but the same expectation values is that you are only applying RZ gates. Qubits are initialized in the |0\rangle state and therefore performing a qml.RZ gate only introduces a phase. This is not reflected when computing the expectation value, as the probability of being in the |0\rangle Z eigenstate (with eingenvalue 1) is still one.

You can try a different circuit and you will observe different expectation values, for example

@qml.qnode(dev)
def circuit(x):
qml.RX(x[:, 0], wires=0)
qml.RX(x[:, 1], wires=1)
exp_vals_z = [qml.expval(qml.PauliZ(wires=w)) for w in [0, 1]]
return exp_vals_z

print(qml.draw(circuit)(x))
x = np.random.randn(5, 2)
print(x)
print(circuit(x))

0: ──RX(M1)──H─┤  <Z>
1: ──RX(M0)────┤  <Z>
[[ 0.13833687 -0.60442826]
[ 0.05236643  0.8771775 ]
[-0.58109614 -0.26983257]
[ 0.94531585  0.9756361 ]
[-0.28200791  0.45957891]]
[[0.         0.         0.         0.         0.        ]
[0.82282715 0.63932402 0.96381554 0.56064144 0.89623936]]


Hope this can help!

2 Likes

Hey everyone, and welcome to the forum @q36111095! @Pablo_Vinas’s answer here is correct . Let’s see the math:

\begin{align*} \vert \psi \rangle = R_z(\theta) \vert 0 \rangle &= e^{-i \theta / 2} \vert 0 \rangle \\ \langle \psi \vert Z \vert \psi \rangle &= \langle 0 \vert e^{i \theta / 2} Z e^{-i \theta / 2} \vert 0 \rangle \\ &= \langle 0 \vert Z \cancelto{1}{e^{-i \theta / 2} e^{i \theta / 2}} \vert 0 \rangle \\ & = \langle 0 \vert Z \vert 0 \rangle \\ & = 1. \end{align*}

But, regarding the 5-dim vectors point, PennyLane will automatically broadcast parameters! This was a feature introduced in v0.24. See our blog post for more details!

2 Likes

Okay, I will try some different gates to introduce features into quantum circuits.
It really helps me!

Sincerely,
Jeff

2 Likes

Oh, I got it!!
The phase will disappear when we measure expectation values of states!
Thanks for your derivation @isaacdevlugt , it really helps me!!

Sincerely,
Jeff

1 Like

Awesome! Glad to hear this worked for you 1 Like