|
9 | 9 | import cirq
|
10 | 10 | import sympy
|
11 | 11 | import numpy as np
|
| 12 | +import pennylane as qml |
12 | 13 | import tensorflow_quantum as tfq
|
13 | 14 | import tensorcircuit as tc
|
14 | 15 |
|
@@ -123,6 +124,42 @@ def op_expectation(params, seed, n_qubits, depth):
|
123 | 124 | )
|
124 | 125 |
|
125 | 126 |
|
| 127 | +def pennylane_approach(n_qubits=10, depth=10, n_circuits=100): |
| 128 | + |
| 129 | + dev = qml.device("lightning.qubit", wires=n_qubits) |
| 130 | + gate_set = [qml.RX, qml.RY, qml.RZ] |
| 131 | + |
| 132 | + @qml.qnode(dev) |
| 133 | + def rand_circuit(params, status): |
| 134 | + for i in range(n_qubits): |
| 135 | + qml.RY(np.pi / 4, wires=i) |
| 136 | + |
| 137 | + for j in range(depth): |
| 138 | + for i in range(n_qubits): |
| 139 | + gate_set[status[i, j]](params[j, i], wires=i) |
| 140 | + |
| 141 | + for i in range(n_qubits - 1): |
| 142 | + qml.CZ(wires=[i, i + 1]) |
| 143 | + |
| 144 | + return qml.expval(qml.Hamiltonian([1.0], [qml.PauliZ(0) @ qml.PauliZ(1)], True)) |
| 145 | + |
| 146 | + gf = qml.grad(rand_circuit, argnum=0) |
| 147 | + params = np.random.uniform(0, 2 * np.pi, size=[n_circuits, depth, n_qubits]) |
| 148 | + status = np.random.choice(3, size=[n_circuits, depth, n_qubits]) |
| 149 | + |
| 150 | + g_results = [] |
| 151 | + |
| 152 | + for i in range(n_circuits): |
| 153 | + g_results.append(gf(params[i], status[i])) |
| 154 | + |
| 155 | + g_results = np.stack(g_results) |
| 156 | + |
| 157 | + return np.std(g_results[:, 0, 0]) |
| 158 | + |
| 159 | + |
| 160 | +benchmark(pennylane_approach) |
| 161 | + |
| 162 | + |
126 | 163 | def tc_approach(n_qubits=10, depth=10, n_circuits=100):
|
127 | 164 |
|
128 | 165 | seed = tc.array_to_tensor(
|
|
0 commit comments