Skip to content

Commit

Permalink
testing of compilation minitools, still variational tools to test, no…
Browse files Browse the repository at this point in the history
…t urgent
  • Loading branch information
KevinMTO committed Apr 26, 2024
1 parent 362dd24 commit 5ada188
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from unittest import TestCase

import numpy as np

from mqt.qudits.compiler.compilation_minitools import new_mod, phi_cost, pi_mod, regulate_theta, rotation_cost_calc, \
swap_elements, \
theta_cost
from mqt.qudits.core import LevelGraph
from mqt.qudits.quantum_circuit import QuantumCircuit


class TestCompilationMiniTools(TestCase):

def test_swap_elements(self):
example = [0, 1, 2, 3]
test_swapped = [3, 1, 2, 0]
swapped_example = swap_elements(example, 0, 3)

self.assertEqual(swapped_example, test_swapped)

def test_pi_mod(self):
res = pi_mod(3 * np.pi / 2)
self.assertEqual(res, -np.pi / 2)

res = pi_mod(-3 * np.pi / 2)
self.assertEqual(res, np.pi / 2)

def test_new_mod(self):
res = new_mod(-5 * np.pi / 2)
self.assertEqual(res, -np.pi / 2)

res = new_mod(5 * np.pi / 2)
self.assertEqual(res, np.pi / 2)

def test_regulate_theta(self):
newang = regulate_theta(-5 * np.pi)
self.assertEqual(newang, -1 * np.pi)

newang = regulate_theta(0.1 * np.pi)
self.assertEqual(newang, 4.1 * np.pi)

def test_theta_cost(self):
cost = theta_cost(np.pi / 8)
self.assertEqual(cost, 6.25e-05)

cost = theta_cost(np.pi / 4)
self.assertEqual(cost, 1.25e-04)

def test_phi_cost(self):
cost = phi_cost(np.pi / 8)
self.assertEqual(cost, 1.25e-05)

cost = phi_cost(np.pi / 4)
self.assertEqual(cost, 2.5e-05)

def test_rotation_cost_calc(self):
test_sample_edges_1 = [(0, 1, {"delta_m": 1, "sensitivity": 1}),
(0, 3, {"delta_m": 0, "sensitivity": 1}),
(4, 3, {"delta_m": 0, "sensitivity": 1}),
(4, 5, {"delta_m": 0, "sensitivity": 1}),
(4, 2, {"delta_m": 0, "sensitivity": 1})
]
test_sample_nodes_1 = [0, 1, 2, 3, 4, 5]
test_sample_nodes_map = [0, 1, 2, 3, 4, 5]

circuit = QuantumCircuit(1, [6], 0)
# NODES CAN BE INFERRED BY THE EDGES
test_graph_1 = LevelGraph(test_sample_edges_1, test_sample_nodes_1, test_sample_nodes_map, [1], 0, circuit)

R_1 = circuit.r(0, [2, 4, np.pi / 4, 0.]) # R(np.pi / 4, 0, 2, 4, 6)
cost_1 = rotation_cost_calc(R_1, test_graph_1)
self.assertEqual(cost_1, 4 * 1.25e-4)

R_2 = circuit.r(0, [3, 4, np.pi / 4, 0.]) # R(np.pi / 4, 0, 3, 4, 6)
cost_2 = rotation_cost_calc(R_2, test_graph_1)
self.assertEqual(cost_2, 3 * 1.25e-4)
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from unittest import TestCase
import numpy as np

from mqt.qudits.compiler.compilation_minitools import UnitaryVerifier
from mqt.qudits.core import LevelGraph
from mqt.qudits.quantum_circuit import QuantumCircuit


class TestUnitaryVerifier(TestCase):

def setUp(self) -> None:
edges = [(0, 3, {"delta_m": 1, "sensitivity": 5}),
(0, 4, {"delta_m": 0, "sensitivity": 3}),
(1, 4, {"delta_m": 0, "sensitivity": 3}),
(1, 2, {"delta_m": 1, "sensitivity": 5})
]
nodes = [0, 1, 2, 3, 4]
nodes_map = [0, 2, 1, 4, 3]
self.circuit = QuantumCircuit(1, [5, 2, 3], 0)
self.graph = LevelGraph(edges, nodes, nodes_map, [0], 0, self.circuit)

def test_verify(self):
dimension = 2

sequence = [self.circuit.cu_one(1, np.identity(dimension, dtype='complex')),
self.circuit.h(1),
self.circuit.h(1)]
target = self.circuit.cu_one(1, np.identity(dimension, dtype='complex'))

nodes = [0, 1]
initial_map = [0, 1]
final_map = [0, 1]

V1 = UnitaryVerifier(sequence, target, [dimension], nodes, initial_map, final_map)

self.assertTrue(V1.verify())

##################################################################

dimension = 3

nodes_3 = [0, 1, 2]
initial_map_3 = [0, 1, 2]
final_map_3 = [0, 2, 1]

sequence_3 = [self.circuit.cu_one(2, np.identity(dimension, dtype='complex')),
self.circuit.h(2),
self.circuit.x(2),
self.circuit.x(2),
self.circuit.x(2)]

target_3 = self.circuit.h(2)

V1 = UnitaryVerifier(sequence_3, target_3, [dimension], nodes_3, initial_map_3, final_map_3)

self.assertTrue(V1.verify())

0 comments on commit 5ada188

Please sign in to comment.