-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed aliasing issue and added tests
- Loading branch information
1 parent
a24f0cf
commit 43109a9
Showing
2 changed files
with
59 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import torchquantum as tq | ||
import qiskit | ||
from qiskit import Aer, execute | ||
|
||
from torchquantum.util import ( | ||
switch_little_big_endian_matrix, | ||
find_global_phase, | ||
) | ||
|
||
from qiskit.circuit.library import GR, GRX, GRY | ||
import numpy as np | ||
|
||
all_pairs = [ | ||
# {"qiskit": GR, "tq": tq.layer.GlobalR, "params": 2}, | ||
{"qiskit": GRX, "tq": tq.layer.GlobalRX, "params": 1}, | ||
{"qiskit": GRY, "tq": tq.layer.GlobalRY, "params": 1}, | ||
] | ||
|
||
ITERATIONS = 10 | ||
|
||
# test each pair | ||
for pair in all_pairs: | ||
# test 2-5 wires | ||
for num_wires in range(2, 5): | ||
# try multiple random parameters | ||
for _ in range(ITERATIONS): | ||
# generate random parameters | ||
params = [ | ||
np.random.uniform(-2 * np.pi, 2 * np.pi) for _ in range(pair["params"]) | ||
] | ||
|
||
# create the qiskit circuit | ||
qiskit_circuit = pair["qiskit"](num_wires, *params) | ||
|
||
# get the unitary from qiskit | ||
backend = Aer.get_backend("unitary_simulator") | ||
result = execute(qiskit_circuit, backend).result() | ||
unitary_qiskit = result.get_unitary(qiskit_circuit) | ||
|
||
# create tq circuit | ||
qdev = tq.QuantumDevice(num_wires) | ||
tq_circuit = pair["tq"](num_wires, *params) | ||
tq_circuit(qdev) | ||
|
||
# get the unitary from tq | ||
unitary_tq = tq_circuit.get_unitary(qdev) | ||
unitary_tq = switch_little_big_endian_matrix(unitary_tq.data.numpy()) | ||
|
||
# phase? | ||
phase = find_global_phase(unitary_tq, unitary_qiskit, 1e-4) | ||
|
||
assert np.allclose( | ||
unitary_tq * phase, unitary_qiskit, atol=1e-6 | ||
), f"{pair} not equal with {params=}!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters