Skip to content

Commit

Permalink
Bugfix release version cannot run KaHyPar (#100)
Browse files Browse the repository at this point in the history
* Now NetworkX is used as the default option for partition, and KaHyPar is left for advanced users

---------

Co-authored-by: PabloAndresCQ <[email protected]>
Co-authored-by: cqc-melf <[email protected]>
  • Loading branch information
3 people authored Apr 17, 2024
1 parent b9c39fc commit 18fae8f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
~~~~~~~~~

0.6.1 (April 2024)
------------------

* When using ``simulate`` with ``TTNxGate`` algorithm, the initial partition is obtained using NetworkX instead of KaHyPar by default. This makes setup easier and means that ``TTNxGate`` can now be used when installing from PyPI. KaHyPar can still be used if ``use_kahypar`` from ``Config`` is set to True.

0.6.0 (April 2024)
------------------

Expand Down
5 changes: 5 additions & 0 deletions pytket/extensions/cutensornet/structured_state/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def __init__(
float_precision: Type[Any] = np.float64,
value_of_zero: float = 1e-16,
leaf_size: int = 8,
use_kahypar: bool = False,
k: int = 4,
optim_delta: float = 1e-5,
loglevel: int = logging.WARNING,
Expand Down Expand Up @@ -113,6 +114,9 @@ def __init__(
``np.float64`` precision (default) and ``1e-7`` for ``np.float32``.
leaf_size: For ``TTN`` simulation only. Sets the maximum number of
qubits in a leaf node when using ``TTN``. Default is 8.
use_kahypar: Use KaHyPar for graph partitioning (used in ``TTN``) if this
is True. Otherwise, use NetworkX (worse, but easy to setup). Defaults
to False.
k: For ``MPSxMPO`` simulation only. Sets the maximum number of layers
the MPO is allowed to have before being contracted. Increasing this
might increase fidelity, but it will also increase resource requirements
Expand Down Expand Up @@ -177,6 +181,7 @@ def __init__(
raise ValueError("Maximum allowed leaf_size is 65.")

self.leaf_size = leaf_size
self.use_kahypar = use_kahypar
self.k = k
self.optim_delta = 1e-5
self.loglevel = loglevel
Expand Down
19 changes: 14 additions & 5 deletions pytket/extensions/cutensornet/structured_state/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ def simulate(
sorted_gates = _get_sorted_gates(circuit, algorithm)

elif algorithm == SimulationAlgorithm.TTNxGate:
qubit_partition = _get_qubit_partition(circuit, config.leaf_size)
qubit_partition = _get_qubit_partition(
circuit, config.leaf_size, config.use_kahypar
)
state = TTNxGate( # type: ignore
libhandle,
qubit_partition,
Expand Down Expand Up @@ -163,7 +165,7 @@ def prepare_circuit_mps(circuit: Circuit) -> tuple[Circuit, dict[Qubit, Qubit]]:


def _get_qubit_partition(
circuit: Circuit, max_q_per_leaf: int
circuit: Circuit, max_q_per_leaf: int, use_kahypar: bool
) -> dict[int, list[Qubit]]:
"""Returns a qubit partition for a TTN.
Expand All @@ -174,6 +176,8 @@ def _get_qubit_partition(
Args:
circuit: The circuit to be simulated.
max_q_per_leaf: The maximum allowed number of qubits per node leaf
use_kahypar: Use KaHyPar for graph partitioning if this is True.
Otherwise, use NetworkX (worse, but easy to setup).
Returns:
A dictionary describing the partition in the format expected by TTN.
Expand Down Expand Up @@ -214,9 +218,14 @@ def _get_qubit_partition(
old_partition = partition.copy()
for key, group in old_partition.items():
# Apply the balanced bisection on this group
(groupA, groupB) = _apply_kahypar_bisection(
connectivity_graph.subgraph(group),
)
if use_kahypar: # Using KaHyPar
(groupA, groupB) = _apply_kahypar_bisection(
connectivity_graph.subgraph(group),
)
else: # Using NetworkX
(groupA, groupB) = nx.community.kernighan_lin_bisection(
connectivity_graph.subgraph(group),
)
# Groups A and B are on the same subtree (key separated by +1)
partition[2 * key] = groupA
partition[2 * key + 1] = groupB
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
license="Apache 2",
packages=find_namespace_packages(include=["pytket.*"]),
include_package_data=True,
install_requires=["pytket ~= 1.26"],
install_requires=["pytket ~= 1.26", "networkx ~= 3.0"],
classifiers=[
"Environment :: Console",
"Programming Language :: Python :: 3.10",
Expand Down
5 changes: 2 additions & 3 deletions tests/test_structured_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def test_copy(algorithm: SimulationAlgorithm) -> None:
simple_circ = Circuit(2).H(0).H(1).CX(0, 1)

with CuTensorNetHandle() as libhandle:

# Default config
cfg = Config()
state = simulate(libhandle, simple_circ, algorithm, cfg)
Expand Down Expand Up @@ -531,15 +530,15 @@ def test_circ_approx_explicit_ttn(circuit: Circuit) -> None:
# Check for TTNxGate
cfg = Config(truncation_fidelity=0.99, leaf_size=3, float_precision=np.float32)
ttn_gate = simulate(libhandle, circuit, SimulationAlgorithm.TTNxGate, cfg)
assert np.isclose(ttn_gate.get_fidelity(), 0.769, atol=1e-3)
assert np.isclose(ttn_gate.get_fidelity(), 0.751, atol=1e-3)
assert ttn_gate.is_valid()
assert np.isclose(ttn_gate.vdot(ttn_gate), 1.0, atol=cfg._atol)

# Fixed virtual bond dimension
# Check for TTNxGate
cfg = Config(chi=120, leaf_size=3, float_precision=np.float32)
ttn_gate = simulate(libhandle, circuit, SimulationAlgorithm.TTNxGate, cfg)
assert np.isclose(ttn_gate.get_fidelity(), 0.857, atol=1e-3)
assert np.isclose(ttn_gate.get_fidelity(), 0.854, atol=1e-3)
assert ttn_gate.is_valid()
assert np.isclose(ttn_gate.vdot(ttn_gate), 1.0, atol=cfg._atol)

Expand Down

0 comments on commit 18fae8f

Please sign in to comment.