hybrid computing with batch #1269
-
Hi, I am trying to perform the calculation of something similar in pennylane on qudaquantum. for i,obs in enumerate(cudaq.observe(self.kernel, spin.z(0), thetas)):
obss[i] = obs.expectation_z() This is time-consuming when batch size is increased. Any way to avoid this? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 2 replies
-
Hi @WeiyinchiangFu, For batched inputs, a typical workflow is this:
Here we are batching over the The If you have a single GPU resource, you would have to parallelize the execution via Let us know if that helps, thanks. |
Beta Was this translation helpful? Give feedback.
-
Hi, If we considier to perform the measurement on multiple qubits, cudaq.observe(self.kernel, [spin.z(0),spin.z(1),spin.z(2)], thetas) Here thetas consists of batched learnable angle(i.e. batch_size=2) and measure on qubit[0], qubit[1] and qubit[2] at the same time. However, I can write this in an alternative way to obtain the results I want, something like for i in range(len(thetas)):
results_one_batch = cudaq.observe(self.kernel, [spin.z(0),spin.z(1),spin.z(2)], thetas[i,:])
m_qubit_0, m_qubit_1, m_qubit_2 = obs[0].expectation(), obs[1].expectation(), obs[2].expectation() But, is this what we expect to deal with the batch measurement on multiple qubits? |
Beta Was this translation helpful? Give feedback.
-
See below code snippet for how to handle multiple measurements on batched inputs:
This code snippet calls In your second code snippet, you call Hope that helps. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Hi, I performed some tests in further. results = cudaq.observe(kernel, [spin.z(0),spin.z(1)], tt)
print([[results[i][j].expectation() for j in range(2)] for i in range(3)])
results = cudaq.observe(kernel, [spin.z(0),spin.z(1),spin.z(2)], tt)
print([[results[i][j].expectation() for j in range(3)] for i in range(3)])
results = cudaq.observe(kernel, [spin.z(0),spin.z(1),spin.z(2),spin.z(3)], tt)
print([[results[i][j].expectation() for j in range(4)] for i in range(3)]) and got results like this
However, when I rewrite the code in an inefficient way, hamiltonian = [spin.z(0),spin.z(1),spin.z(2),spin.z(3)]
for i in range(3):
results = []
for jdx, j in enumerate(hamiltonian):
tmp = cudaq.observe(kernel, j , tt[i]).expectation()
results.append(tmp)
print(results) I got
Did I do something wrong? |
Beta Was this translation helpful? Give feedback.
-
Here is the definition of the circuit. layer = 2
n_qubits = 4
kernel, theta = cudaq.make_kernel(list)
qubits = kernel.qalloc(n_qubits)
r_ang = torch.arcsin(torch.rand(2)*2 -1)
w = theta
for i in range(n_qubits):
kernel.ry(r_ang[0],qubits[i])
kernel.rz(r_ang[1],qubits[i])
for l in range(layer):
for i in range(n_qubits):
kernel.ry(parameter=w[i],target=qubits[i])
for i in range(n_qubits-1):
kernel.cx(control=qubits[i], target=qubits[i+1])
kernel.rz(parameter=w[i+n_qubits], target=qubits[i+1])
kernel.cx(control=qubits[i], target=qubits[i+1]) and tt = torch.rand(3,7, device=device) These are the tests I have done. results = cudaq.observe(kernel, [spin.z(0),spin.z(1)], tt)
print([[results[i][j].expectation() for j in range(2)] for i in range(3)])
results = cudaq.observe(kernel, [spin.z(0),spin.z(1),spin.z(2)], tt)
print([[results[i][j].expectation() for j in range(3)] for i in range(3)])
results = cudaq.observe(kernel, [spin.z(0),spin.z(1),spin.z(2),spin.z(3)], tt)
print([[results[i][j].expectation() for j in range(4)] for i in range(3)]) and obtained
hamiltonian = [spin.z(0),spin.z(1),spin.z(2),spin.z(3)]
for i in range(3):
results = []
for jdx, j in enumerate(hamiltonian):
tmp = cudaq.observe(kernel, j , tt[i]).expectation()
results.append(tmp)
print(results) obtained
hamiltonian = [spin.z(0),spin.z(1),spin.z(2),spin.z(3)]
results = []
for jdx, j in enumerate(hamiltonian):
tmp = cudaq.observe(kernel, j , tt)
results.append([tmp[i].expectation() for i in range(3)])
for i in range(3):
print([results[j][i] for j in range(len(hamiltonian))]) obtained
My question is how we simultaneously deal with the multi-qubit measurement and the batched input. When I fed the input data and performed the measurement on more than two qubits, weird things happened. |
Beta Was this translation helpful? Give feedback.
-
Hi @WeiyinchiangFu, apologies for the delay in replying. I am taking a look at this for you and will get back to you soon. Thanks |
Beta Was this translation helpful? Give feedback.
-
Hi @WeiyinchiangFu, this has been fixed with this PR. Please pull the latest container once it's merged into main and test. Apologies for the delay in getting back to you. |
Beta Was this translation helpful? Give feedback.
Hi @WeiyinchiangFu,
For batched inputs, a typical workflow is this:
Here we are batching over the
num_of_samples
. The finalfor
loop is required and can't be avoided.The
observe
call is where the execution of the quantum circui…