Skip to content

Commit

Permalink
Add partition test
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-be committed Nov 2, 2023
1 parent c612f6b commit 1be4c36
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from qiskit.converters import circuit_to_dagdependency
from qiskit.dagcircuit import DAGDependency

PATH_KAHYPAR_CONFIG = "./../kahypar.ini"


def dag_to_hypergraph(dag: DAGDependency, k=2):
n = len(dag.qubits)
Expand Down Expand Up @@ -50,7 +52,7 @@ def get_partitions(circuit_or_dag, num_partitions, max_partition_size, verbose=F
raise TypeError("Input has to be either a QuantumCircuit or DAGDependency")
hypergraph = dag_to_hypergraph(dag, num_partitions)
context = kahypar.Context()
context.loadINIconfiguration("kahypar.ini")
context.loadINIconfiguration(PATH_KAHYPAR_CONFIG)
context.setK(num_partitions)
if isinstance(max_partition_size, int):
max_partition_size = [max_partition_size for _ in range(num_partitions)]
Expand Down
32 changes: 32 additions & 0 deletions test/test_partition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
import numpy as np
from qiskit.circuit.library import EfficientSU2

from app.partition import get_partitions


class PartitionTestCase(unittest.TestCase):
def test_get_partitions(self):
num_qubits = 7
num_partitions = 2
circuit = EfficientSU2(
num_qubits=num_qubits,
reps=2,
entanglement="full",
su2_gates=["ry"],
)
circuit = circuit.decompose()

params = [(np.pi * i) / 16 for i in range(len(circuit.parameters))]
circuit = circuit.bind_parameters(params)
partitions, partition_sizes = get_partitions(
circuit,
num_partitions=num_partitions,
max_partition_size=int(np.ceil(num_qubits / 2)),
)
print(partitions)
print(partition_sizes)
self.assertEquals(len(partition_sizes), num_partitions)
self.assertTrue(
all([size <= np.ceil(num_qubits / 2) for size in partition_sizes])
)

0 comments on commit 1be4c36

Please sign in to comment.