Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic TTN simulation #41

Merged
merged 93 commits into from
Feb 2, 2024
Merged
Changes from 4 commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
19273db
Created new general.py file for logger and cutensornet handle. Update…
PabloAndresCQ Sep 25, 2023
2fde81a
Setting logger at MPS. Also changed my mind and restored CuTensornetH…
PabloAndresCQ Sep 25, 2023
beded34
Added debugging and info logging messages.
PabloAndresCQ Sep 26, 2023
fd362e1
Forgot to add the general.py file
PabloAndresCQ Sep 26, 2023
6ebe2d0
Improved messages
PabloAndresCQ Sep 26, 2023
b348f14
Fixed a bug on simulate I just found.
PabloAndresCQ Sep 26, 2023
cc92c04
Fixed linting and failing test
PabloAndresCQ Sep 26, 2023
82bc9ed
Merge branch 'develop' into feature/mps_logger
PabloAndresCQ Sep 27, 2023
f8b41d6
Updated Jupyter notebook with example of logger.
PabloAndresCQ Sep 27, 2023
deee46e
Now using a ConfigMPS object to provide the simulation settings.
PabloAndresCQ Sep 27, 2023
75358db
Merge branch 'feature/mps_logger' into refactor/mps_arguments
PabloAndresCQ Sep 27, 2023
b8ee244
Added value_of_zero parameter.
PabloAndresCQ Sep 28, 2023
14353fc
Small changes
PabloAndresCQ Sep 28, 2023
460736c
Fixing docs
PabloAndresCQ Sep 28, 2023
7ef38b5
Merge branch 'develop' into refactor/mps_arguments
PabloAndresCQ Sep 29, 2023
5b620d9
Fixed some issues when merging
PabloAndresCQ Sep 29, 2023
0d0af5c
Updated Jupyter notebook
PabloAndresCQ Oct 3, 2023
2517ab5
Warning now using warnings module
PabloAndresCQ Oct 3, 2023
a16ec7e
Moved MPS code to make room for TTN.
PabloAndresCQ Oct 10, 2023
0647c39
Restoring source code in /states folder
PabloAndresCQ Oct 10, 2023
1760257
Moved ConfigMPS to Config in general
PabloAndresCQ Oct 10, 2023
014a173
Some fixes
PabloAndresCQ Oct 10, 2023
3b71b65
Some more fixes
PabloAndresCQ Oct 10, 2023
a465474
Moving some more things around
PabloAndresCQ Oct 10, 2023
47fd903
Began implementation of TTN
PabloAndresCQ Oct 10, 2023
b86a545
Added single-qubit gate support and some basic tests.
PabloAndresCQ Oct 11, 2023
68401ce
Fixed some bugs
PabloAndresCQ Oct 11, 2023
de3dccb
Added vdot to TTN
PabloAndresCQ Oct 12, 2023
c00f975
Some bugs fixed
PabloAndresCQ Oct 12, 2023
eeab27c
Implemented get_amplitude and get_statevector. Current tests passing.
PabloAndresCQ Oct 12, 2023
fd79ef9
Implemented canonicalisation
PabloAndresCQ Oct 12, 2023
ff1dbf9
Renamed module states to tnstate
PabloAndresCQ Oct 12, 2023
ba6c4e8
Some basic fixes
PabloAndresCQ Oct 13, 2023
4528898
In the process of debugging canonicalisation
PabloAndresCQ Oct 17, 2023
2fbc559
Canonicalisation debugged
PabloAndresCQ Oct 18, 2023
13fffe3
In the process of implementing _apply_2q_gate
PabloAndresCQ Oct 18, 2023
1e907da
Exact 2-qubit gate application implemented. Not debugged yet.
PabloAndresCQ Oct 19, 2023
cfb15a6
Exact apply 2q gate implemented and debugged
PabloAndresCQ Oct 19, 2023
e1ca882
Added truncation by chi and value_of_zero
PabloAndresCQ Oct 19, 2023
19fea04
Small changes to debugging messages
PabloAndresCQ Oct 19, 2023
d38451e
Approximate sim under chi implemented and tested.
PabloAndresCQ Oct 20, 2023
f839f15
Added some more computationally intensive tests
PabloAndresCQ Oct 20, 2023
c2cd8ce
Unified TTN and MPS via TNState abstract class
PabloAndresCQ Oct 22, 2023
7ded611
_get_qubit_partition now does recursive balanced bisections.
PabloAndresCQ Nov 1, 2023
c2c47f9
Merged develop into branch
PabloAndresCQ Nov 1, 2023
4161474
Updated cq.contract providing handles and contraction path when known
PabloAndresCQ Nov 1, 2023
bc9298a
Changed default leaf size
PabloAndresCQ Nov 1, 2023
b295937
Changed the leaf_size parameter of the explicit_ttn test
PabloAndresCQ Nov 1, 2023
ca2fab8
Removing unused imports and pleasing linter.
PabloAndresCQ Nov 1, 2023
5f5453a
Changed docstrings of Config
PabloAndresCQ Nov 1, 2023
8f551bd
Updated docs
PabloAndresCQ Nov 1, 2023
df9db66
Updated the _get_sorted_gates algorithm so that it's compatible with …
PabloAndresCQ Nov 2, 2023
405f951
Fidelity slightly improved in one of the tests
PabloAndresCQ Nov 2, 2023
5d4edf2
Changes suggested by Iakov.
PabloAndresCQ Nov 7, 2023
9d0a284
Removed unused imports
PabloAndresCQ Nov 7, 2023
9308bfe
Changes suggested by Iakov
PabloAndresCQ Nov 14, 2023
193aed9
Jupyter notebook updated
PabloAndresCQ Nov 15, 2023
f1c9678
Updated MPI example
PabloAndresCQ Nov 16, 2023
b2a0b48
Replacing KL partitioner with KaHyPar
PabloAndresCQ Jan 10, 2024
aa3df63
Bugfixes
PabloAndresCQ Jan 10, 2024
df01841
Update copyright message
PabloAndresCQ Jan 12, 2024
f5cdc34
Bugfix on bounded chi truncation due to towards_root flag not being u…
PabloAndresCQ Jan 13, 2024
6c7592c
Fixing small mistake from previous commit
PabloAndresCQ Jan 13, 2024
5e8216d
Code readability improvement. Moved chi truncation implementation to …
PabloAndresCQ Jan 15, 2024
acd39a4
Added truncation_fidelity approach to TTNxGate
PabloAndresCQ Jan 15, 2024
4395cc1
Updated tests
PabloAndresCQ Jan 15, 2024
f420268
Removed NotImplementedError exception and updated the fidelity of app…
PabloAndresCQ Jan 15, 2024
4390ecb
Linting
PabloAndresCQ Jan 16, 2024
d934184
Implemented a different approach for gate fidelity truncation.
PabloAndresCQ Jan 23, 2024
ec5c2cb
Update fidelity test
PabloAndresCQ Jan 23, 2024
924146c
Added some extra debug messages to logger
PabloAndresCQ Jan 23, 2024
f8777cc
Refactored the code to avoid the use of funnel tensors.
PabloAndresCQ Jan 27, 2024
c9fa77f
Some minor fixes and refactoring
PabloAndresCQ Jan 27, 2024
2035091
Fixing a leaf_size in tests so that we test on TTNs of height >2
PabloAndresCQ Jan 27, 2024
10e04b0
Added some logger messages
PabloAndresCQ Jan 27, 2024
d5008b7
Merge pull request #69 from CQCL/feature/ttn_kahypar
PabloAndresCQ Feb 1, 2024
240d9c0
Fixing typo in comment
PabloAndresCQ Feb 1, 2024
c357532
Updated docstring.
PabloAndresCQ Feb 1, 2024
943f9f3
Merge branch 'feature/ttn' into feature/ttn_gate_fidelity
PabloAndresCQ Feb 1, 2024
6c0fcb4
Merge pull request #74 from CQCL/feature/ttn_gate_fidelity
PabloAndresCQ Feb 1, 2024
f35fcc3
Fixed an unfinished comment
PabloAndresCQ Feb 1, 2024
7619bcd
Merge branch 'feature/ttn' into feature/ttn_no_funnels
PabloAndresCQ Feb 1, 2024
2f86a77
Merge pull request #78 from CQCL/feature/ttn_no_funnels
PabloAndresCQ Feb 1, 2024
96cca32
Cleaned up code around bonds_from_q0_to_ancestor
PabloAndresCQ Feb 1, 2024
6416d5b
Merge branch 'develop' into feature/ttn
PabloAndresCQ Feb 1, 2024
3f1a946
Applying lint, mypy and updating explicit fidelity in test
PabloAndresCQ Feb 1, 2024
1221eac
Renamed tnstate to structured_state
PabloAndresCQ Feb 2, 2024
9043661
Linting and mypy
PabloAndresCQ Feb 2, 2024
bdab46a
Updated module docstring
PabloAndresCQ Feb 2, 2024
4b0ae27
Changed kahypar to strict version requirement
PabloAndresCQ Feb 2, 2024
94b03fe
Removed kahypar dependency
PabloAndresCQ Feb 2, 2024
0206d17
Flexible kahypar import
PabloAndresCQ Feb 2, 2024
d96e37f
Linter being pedantic
PabloAndresCQ Feb 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pytket/extensions/cutensornet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
2 changes: 1 addition & 1 deletion pytket/extensions/cutensornet/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
2 changes: 1 addition & 1 deletion pytket/extensions/cutensornet/general.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
2 changes: 1 addition & 1 deletion pytket/extensions/cutensornet/tensor_network_convert.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
2 changes: 1 addition & 1 deletion pytket/extensions/cutensornet/tnstate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
64 changes: 64 additions & 0 deletions pytket/extensions/cutensornet/tnstate/cut_rKaHyPar_sea20.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# general
mode=recursive
objective=cut
seed=-1
cmaxnet=-1
vcycles=0
# main -> preprocessing -> min hash sparsifier
p-use-sparsifier=true
p-sparsifier-min-median-he-size=28
p-sparsifier-max-hyperedge-size=1200
p-sparsifier-max-cluster-size=10
p-sparsifier-min-cluster-size=2
p-sparsifier-num-hash-func=5
p-sparsifier-combined-num-hash-func=100
# main -> preprocessing -> community detection
p-detect-communities=true
p-detect-communities-in-ip=false
p-reuse-communities=false
p-max-louvain-pass-iterations=100
p-min-eps-improvement=0.0001
p-louvain-edge-weight=hybrid
p-large-he-threshold=1000
# main -> preprocessing -> large he removal
p-smallest-maxnet-threshold=50000
p-maxnet-removal-factor=0.01
# main -> coarsening
c-type=heavy_lazy
c-s=3.25
c-t=160
# main -> coarsening -> rating
c-rating-score=heavy_edge
c-rating-use-communities=true
c-rating-heavy_node_penalty=multiplicative
c-rating-acceptance-criterion=best
c-fixed-vertex-acceptance-criterion=free_vertex_only
# main -> initial partitioning
i-mode=direct
i-technique=flat
# initial partitioning -> initial partitioning
i-algo=pool
i-runs=20
# initial partitioning -> bin packing
i-bp-algorithm=worst_fit
i-bp-heuristic-prepacking=false
i-bp-early-restart=true
i-bp-late-restart=true
# initial partitioning -> local search
i-r-type=twoway_fm
i-r-runs=-1
i-r-fm-stop=simple
i-r-fm-stop-i=50
# main -> local search
r-type=twoway_fm_hyperflow_cutter
r-runs=-1
r-fm-stop=adaptive_opt
r-fm-stop-alpha=1
r-fm-stop-i=350
# local_search -> flow scheduling and heuristics
r-flow-execution-policy=exponential
# local_search -> hyperflowcutter configuration
r-hfc-size-constraint=mf-style
r-hfc-scaling=16
r-hfc-distance-based-piercing=true
r-hfc-mbc=true
2 changes: 1 addition & 1 deletion pytket/extensions/cutensornet/tnstate/general.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
2 changes: 1 addition & 1 deletion pytket/extensions/cutensornet/tnstate/mps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
2 changes: 1 addition & 1 deletion pytket/extensions/cutensornet/tnstate/mps_gate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
2 changes: 1 addition & 1 deletion pytket/extensions/cutensornet/tnstate/mps_mpo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
76 changes: 69 additions & 7 deletions pytket/extensions/cutensornet/tnstate/simulation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -14,10 +14,12 @@
from typing import Optional
from enum import Enum

from pathlib import Path
from collections import defaultdict # type: ignore
import numpy as np # type: ignore

from networkx import Graph, community # type: ignore
import networkx as nx # type: ignore
import kahypar # type: ignore

from pytket.circuit import Circuit, Command, Qubit
from pytket.transform import Transform
@@ -194,23 +196,21 @@ def _get_qubit_partition(
)

# Create the connectivity graph in NetworkX
connectivity_graph = Graph()
connectivity_graph = nx.Graph()
connectivity_graph.add_nodes_from(circuit.qubits)
for (u, v), weight in edge_weights.items():
connectivity_graph.add_edge(u, v, weight=weight)

# Apply balanced bisections until each qubit group is small enough
partition = {0: set(circuit.qubits)}
partition = {0: circuit.qubits}
stop_bisec = False # Do at least one bisection (TTN reqs >1 leaf nodes)

while not stop_bisec:
old_partition = partition.copy()
for key, group in old_partition.items():
# Apply the balanced bisection on this group
(groupA, groupB) = community.kernighan_lin_bisection(
(groupA, groupB) = _apply_kahypar_bisection(
connectivity_graph.subgraph(group),
max_iter=2 * len(group), # Iteractions scaling with number of nodes
weight="weight",
)
# Groups A and B are on the same subtree (key separated by +1)
partition[2 * key] = groupA
@@ -223,6 +223,68 @@ def _get_qubit_partition(
return qubit_partition


def _apply_kahypar_bisection(
graph: nx.Graph,
) -> tuple[list[Qubit], list[Qubit]]:
"""Use KaHyPar to obtain a bisection of the graph.

Returns:
Two lists, each containing the vertices in either group of the bisection.
"""
vertices = list(graph.nodes)
edges = list(graph.edges)
weight_dict = nx.get_edge_attributes(graph, "weight")
qubit_dict = {q: i for i, q in enumerate(vertices)}

num_vertices = len(vertices)
num_edges = len(edges)
k = 2 # Number of groups in the partition
epsilon = 0.03 # Imbalance tolerance

# Special case where the graph has no edges; KaHyPar cannot deal with it
if num_edges == 0:
# Just split the list of vertices in half
return (vertices[: num_vertices // 2], vertices[num_vertices // 2 :])

# KaHyPar expects the list of edges to be provided as a continuous set of vertices
# ``edge_stream`` where ``edge_indices`` indicates where each new edge begins
# (the latter is necessary because KaHyPar can accept hyperedges)
edge_stream = [qubit_dict[vertex] for edge in edges for vertex in edge]
edge_indices = [0] + [2 * (i + 1) for i in range(num_edges)]
edge_weights = [weight_dict[edge] for edge in edges]
vertex_weights = [1 for _ in range(num_vertices)]

hypergraph = kahypar.Hypergraph(
num_vertices,
num_edges,
edge_indices,
edge_stream,
k,
edge_weights,
vertex_weights,
)

# Set up the configuration for KaHyPar
context = kahypar.Context()
context.setK(k)
context.setEpsilon(epsilon)
context.suppressOutput(True)

# Load the default configuration file provided by the KaHyPar devs
ini_file = str(Path(__file__).parent / "cut_rKaHyPar_sea20.ini")
context.loadINIconfiguration(ini_file)

# Run the partitioner
kahypar.partition(hypergraph, context)
partition_dict = {i: hypergraph.blockID(i) for i in range(hypergraph.numNodes())}

# Obtain the two groups of qubits from ``partition_dict``
groupA = [vertices[i] for i, block in partition_dict.items() if block == 0]
groupB = [vertices[i] for i, block in partition_dict.items() if block == 1]

return (groupA, groupB)


def _get_sorted_gates(
circuit: Circuit,
algorithm: SimulationAlgorithm,
2 changes: 1 addition & 1 deletion pytket/extensions/cutensornet/tnstate/ttn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
2 changes: 1 addition & 1 deletion pytket/extensions/cutensornet/tnstate/ttn_gate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2023 Quantinuum
# Copyright 2019-2024 Quantinuum
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@
license="Apache 2",
packages=find_namespace_packages(include=["pytket.*"]),
include_package_data=True,
install_requires=["pytket ~= 1.11"],
install_requires=["pytket ~= 1.11", "kahypar ~= 1.3.5"],
classifiers=[
"Environment :: Console",
"Programming Language :: Python :: 3.9",
2 changes: 1 addition & 1 deletion tests/test_tnstate.py
Original file line number Diff line number Diff line change
@@ -487,7 +487,7 @@ def test_circ_approx_explicit_ttn(circuit: Circuit) -> None:
ttn_gate = simulate(libhandle, circuit, SimulationAlgorithm.TTNxGate, cfg)
for g in circuit.get_commands():
ttn_gate.apply_gate(g)
assert np.isclose(ttn_gate.get_fidelity(), 0.62, atol=1e-2)
assert np.isclose(ttn_gate.get_fidelity(), 0.64, atol=1e-2)
assert ttn_gate.is_valid()
assert np.isclose(ttn_gate.vdot(ttn_gate), 1.0, atol=cfg._atol)