Hi! I just started to use pennylane in a few months. I’m running two different quantum functions as follows:
import pennylane as qml
from qiskit.quantum_info import random_clifford, Operator
device1 = qml.device("default.qubit", wires=20, shots=100)
@qml.qnode(device1)
def GHZState1(n_qubits, clifford):
# ----------- GHZ State -------------- #
qml.Hadamard(wires=0)
for qbit in range(n_qubits - 1):
qml.CNOT(wires=[qbit, qbit+1])
# ----------- GHZ State -------------- #
qml.QubitUnitary(clifford, wires=n_qubits-1)
return [qml.sample(qml.PauliZ(i)) for i in range(n_qubits)]
clifford = Operator(random_clifford(1)).data
out = GHZState1(20, clifford)
And another function is:
import pennylane as qml
from qiskit.quantum_info import random_clifford, Operator
device2 = qml.device("default.qubit", wires=20, shots=1)
@qml.qnode(device2)
def GHZState2(n_qubits, clifford, random_unitary):
# ----------- GHZ State -------------- #
qml.Hadamard(wires=0)
for qbit in range(n_qubits - 1):
qml.CNOT(wires=[qbit, qbit+1])
# ----------- GHZ State -------------- #
qml.QubitUnitary(clifford, wires=n_qubits-1)
return [qml.sample(qml.PauliZ(i)) for i in range(n_qubits)]
sampled_cliffords = []
for _ in range(n_rho):
sampled_clifford = random_clifford(1)
sampled_clifford = Operator(sampled_clifford).data
sampled_cliffords.append(sampled_clifford)
sampled_cliffords = np.stack(sampled_cliffords, axis=0)
out = GHZState2(20, sampled_cliffords)
You can see that the circuits are the same, the only difference is whether to use parameter broadcast. I calculated the running time and I found the first one without parameter broadcasting is much faster than the other, e.g., 0.2s vs 6.4s. I wonder the reason.
Besides, what should I do if there is an if
statement in my function when I try to use parameter broadcast? For example, the circuit is:
def GHZState_if(n_qubits, use_clifford, clifford, random_unitary):
# ----------- GHZ State -------------- #
qml.Hadamard(wires=0)
for qbit in range(n_qubits - 1):
qml.CNOT(wires=[qbit, qbit+1])
# ----------- GHZ State -------------- #
if use_clifford:
qml.QubitUnitary(clifford, wires=n_qubits-1)
return [qml.sample(qml.PauliZ(i)) for i in range(n_qubits)]
The variable use_clifford
is a shape (n,) array if clifford
is a shape (n,2,2) array, and they correspond to n different circuits. I want to use parameter broadcast to simulate circuits under different situations since I don’t want to use for loop in my function. Is there a possible solution to achieve it?
Moreover, does pennylane have the function to generate a Clifford gate like qiskit.quantum_info.random_clifford?