Skip to content

Commit

Permalink
[minor] preliminary tests for inverse
Browse files Browse the repository at this point in the history
  • Loading branch information
GenericP3rson committed Apr 30, 2024
1 parent d9b8d70 commit 7e05eea
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/module/inverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import torchquantum as tq
from torchquantum.plugin import op_history2qiskit, qiskit2tq_op_history
from torchquantum.measurement import expval_joint_analytical
from qiskit import QuantumCircuit, Aer, execute
from qiskit.quantum_info import Pauli
import numpy as np

"""
Testing strategy:
partition on Operation: iterate through all the possible operations
partition on number of gates in module: 1, >1
"""

def compare(ops, n_wires):
# construct a normal tq circuit
qmod = tq.QuantumModule.from_op_history(ops)
qdev = tq.QuantumDevice(n_wires=n_wires, record_op=True)
qmod(qdev)

# turn into qiskit and inverse
qiskit_circuit = op_history2qiskit(n_wires, qdev.op_history)
qiskit_circuit = qiskit_circuit.inverse()

# inverse the tq circuit
qmod = tq.QuantumModule.from_op_history(ops)
qdev = tq.QuantumDevice(n_wires=n_wires, record_op=True)
qmod.inverse_module()
qmod(qdev)

qdev_ops = qiskit2tq_op_history(qiskit_circuit)

for tq_op, qiskit_op in zip(qdev.op_history, qdev_ops):
# TODO: name-wise (but currently need to ensure, e.g., cx == cnot)
if tq_op["params"] is not None and qiskit_op["params"] is not None:
assert np.allclose(np.array(tq_op["params"]), np.array(qiskit_op["params"]))

def get_random_rotations(num_params):
return 4*np.pi*np.random.rand(num_params) - 2*np.pi

def test_inverse():
ops = [
{'name': 'u3', 'wires': 0, 'trainable': True, 'params': get_random_rotations(3)},
{'name': 'u3', 'wires': 1, 'trainable': True, 'params': get_random_rotations(3)},
{'name': 'cx', 'wires': [0, 1]},
{'name': 'cx', 'wires': [1, 0]},
{'name': 'u3', 'wires': 0, 'trainable': True, 'params': get_random_rotations(3)},
{'name': 'u3', 'wires': 1, 'trainable': True, 'params': get_random_rotations(3)},
{'name': 'cx', 'wires': [0, 1]},
{'name': 'cx', 'wires': [1, 0]},
]
compare(ops, 2)

# test_inverse()

0 comments on commit 7e05eea

Please sign in to comment.