-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from GenericP3rson/dev
Merging Updated Dev
- Loading branch information
Showing
16 changed files
with
1,376 additions
and
723 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
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
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,105 @@ | ||
import torchquantum as tq | ||
from qiskit.circuit.library import ( | ||
TwoLocal, | ||
EfficientSU2, | ||
ExcitationPreserving, | ||
PauliTwoDesign, | ||
RealAmplitudes, | ||
) | ||
|
||
|
||
def compare_tq_to_qiskit(tq_circuit, qiskit_circuit, instance_info=""): | ||
""" | ||
helper function to compare if tq and qiskit have the same gates configuration | ||
""" | ||
|
||
qiskit_ops = [] | ||
for bit in qiskit_circuit.decompose(): | ||
wires = [] | ||
for qu in bit.qubits: | ||
wires.append(qu.index) | ||
qiskit_ops.append( | ||
{ | ||
"name": bit.operation.name, | ||
"wires": tuple(wires), | ||
} | ||
) | ||
|
||
# create operations list | ||
tq_ops = [ | ||
{ | ||
"name": op["name"], | ||
"wires": (op["wires"],) | ||
if isinstance(op["wires"], int) | ||
else tuple(op["wires"]), | ||
} | ||
for op in tq_circuit.op_history | ||
] | ||
|
||
# create tuples, preserving order | ||
tq_ops_tuple = [tuple(op) for op in tq_ops] | ||
qiskit_ops_tuple = [tuple(op) for op in qiskit_ops] | ||
|
||
# assert if they are the same | ||
assert len(tq_ops) == len( | ||
qiskit_ops | ||
), f"operations are varying lengths for {instance_info}" | ||
assert ( | ||
tq_ops_tuple == qiskit_ops_tuple | ||
), f"operations do not match for {instance_info}" | ||
|
||
|
||
## TEST TWOLOCAL | ||
|
||
|
||
def test_twolocal(): | ||
# iterate through different parameters to test | ||
for entanglement_type in ("linear", "circular", "full"): | ||
for n_wires in (3, 5, 10): | ||
for reps in range(1, 5): | ||
# create the TQ circuit | ||
tq_two = tq.layer.TwoLocal( | ||
n_wires, | ||
["ry", "rz"], | ||
"cz", | ||
entanglement_layer=entanglement_type, | ||
reps=reps, | ||
) | ||
qdev = tq.QuantumDevice(n_wires, record_op=True) | ||
tq_two(qdev) | ||
|
||
# create the qiskit circuit | ||
qiskit_two = TwoLocal( | ||
n_wires, | ||
["ry", "rz"], | ||
"cz", | ||
entanglement_type, | ||
reps=reps, | ||
insert_barriers=False, | ||
) | ||
|
||
# compare the circuits | ||
test_info = f"{entanglement_type} with {n_wires} wires and {reps} reps" | ||
compare_tq_to_qiskit(qdev, qiskit_two) | ||
|
||
|
||
## TEST OTHER CIRCUITS | ||
|
||
|
||
def test_twolocal_variants(): | ||
tq_to_qiskit = { | ||
"EfficientSU2": (tq.layer.EfficientSU2, EfficientSU2), | ||
"ExcitationPreserving": (tq.layer.ExcitationPreserving, ExcitationPreserving), | ||
"RealAmplitudes": (tq.layer.RealAmplitudes, RealAmplitudes), | ||
"PauliTwo": (tq.layer.PauliTwoDesign, PauliTwoDesign), | ||
} | ||
|
||
# run all the tests | ||
for circuit_name in tq_to_qiskit: | ||
tq_instance, qiskit_instance = tq_to_qiskit[circuit_name] | ||
for n_wires in range(2, 5): | ||
tq_circuit = tq_instance(n_wires) | ||
circuit = qiskit_instance(n_wires) | ||
qdev = tq.QuantumDevice(n_wires, record_op=True) | ||
tq_circuit(qdev) | ||
compare_tq_to_qiskit(qdev, circuit, f"{circuit_name} with {n_wires} wires") |
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,55 @@ | ||
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, GRZ | ||
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}, | ||
{"qiskit": GRZ, "tq": tq.layer.GlobalRZ, "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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ | |
|
||
from .layers import * | ||
from .nlocal import * | ||
from .general import * |
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,104 @@ | ||
""" | ||
MIT License | ||
Copyright (c) 2020-present TorchQuantum Authors | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
""" | ||
|
||
|
||
import torch | ||
import torchquantum as tq | ||
from torchquantum.layer.layers import ( | ||
LayerTemplate0, | ||
Op1QAllLayer, | ||
Op2QAllLayer, | ||
RandomOp1All, | ||
) | ||
|
||
__all__ = [ | ||
"GlobalR", | ||
"GlobalRX", | ||
"GlobalRY", | ||
"GlobalRZ", | ||
] | ||
|
||
|
||
class GlobalR(tq.QuantumModule): | ||
"""Layer Template for a Global R General Gate""" | ||
|
||
def __init__( | ||
self, | ||
n_wires: int = 0, | ||
theta: float = 0, | ||
phi: float = 0, | ||
): | ||
"""Create the layer""" | ||
super().__init__() | ||
self.n_wires = n_wires | ||
self.params = torch.tensor([[theta, phi]]) | ||
|
||
@tq.static_support | ||
def forward(self, q_device, x=None): | ||
for k in range(self.n_wires): | ||
tq.R()(q_device, wires=k, params=self.params) | ||
|
||
|
||
class GlobalRX(GlobalR): | ||
"""Layer Template for a Global RX General Gate""" | ||
|
||
def __init__( | ||
self, | ||
n_wires: int = 0, | ||
theta: float = 0, | ||
): | ||
"""Create the layer""" | ||
super().__init__(n_wires, theta, phi=0) | ||
|
||
|
||
class GlobalRY(GlobalR): | ||
"""Layer Template for a Global RY General Gate""" | ||
|
||
def __init__( | ||
self, | ||
n_wires: int = 0, | ||
theta: float = 0, | ||
): | ||
"""Create the layer""" | ||
super().__init__(n_wires, theta, phi=torch.pi / 2) | ||
|
||
class GlobalRZ(tq.QuantumModule): | ||
"""Layer Template for a Global RZ General Gate""" | ||
|
||
def __init__( | ||
self, | ||
n_wires: int = 0, | ||
phi: float = 0, | ||
): | ||
"""Create the layer""" | ||
super().__init__() | ||
self.n_wires = n_wires | ||
self.params = torch.tensor([[phi]]) | ||
|
||
@tq.static_support | ||
def forward(self, q_device, x=None): | ||
for k in range(self.n_wires): | ||
tq.RZ()(q_device, wires=k, params=self.params) | ||
|
||
|
Oops, something went wrong.