Skip to content

Add test suite that runs anatevka on large graphs with known MWPMs #60

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

Merged
merged 2 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
*.fasl
.DS_Store
*~
.idea/
.ipynb_checkpoints/
.python-version
__pycache__
3 changes: 2 additions & 1 deletion anatevka-tests.asd
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
(:file "contract")
(:file "multireweight")
(:file "reweight")))
(:file "blossom")))
(:file "blossom")
(:file "cases")))
45 changes: 27 additions & 18 deletions tests/blossom.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@
(iterations +default-iterations+)
(timeout +default-timeout+)
(timestep +default-timestep+)
(dryad-class 'dryad)) &body body)
(dryad-class 'dryad)
(solution-weight nil)) &body body)
"Used to define a blossom algorithm unit test named `TEST-NAME'. The LIST of `COORDINATES' represents the problem graph to be fed to the blossom algorithm.

There are also a collection of optional keyword arguments that allow individual tests to be customized. The `DEBUG?' and `DRYAD-CLOCK-RATE' parameters set the process debug flag and the process clock rate, respectively, of the DRYAD in the algorithm. The `ITERATIONS' parameter determines how many times the test will be run. The `BORDER' parameter offsets the coordinates. The `TIMEOUT' parameter determines how long the test can run before it is considered a failure. The `TIMESTEP' parameter designates how many clock cycles to run between each RECEIVE-MESSAGE call. The `DRYAD-CLASS' parameter allows this test suite to be used with different types of dryads.
Expand All @@ -191,10 +192,20 @@ Finally, the `BODY' contains optional declarations and a docstring, and then is
NOTE: This macro automatically rescales the pairs in `COORDINATES' to reside at measurement qubit locations in a suitably large instance of the surface code."
(multiple-value-bind (solutions decls docstring)
(alexandria:parse-body body :documentation t)
(assert (apply #'= (mapcar #'matching-weight solutions))
(unless (null (first solutions))
(assert (apply #'= (mapcar #'matching-weight solutions))
()
"Solutions differ in weight.")
(when solution-weight
(assert (= solution-weight (matching-weight (first solutions)))
()
"Provided solutions have weight ~a but :solution-weight is ~a."
(matching-weight (first solutions)) solution-weight)))
(assert (or (first solutions) solution-weight)
()
"Solutions differ in weight.")
(let ((solution-weight (matching-weight (first solutions))))
"Must provide at least one solution or a :solution-weight.")
(let ((solution-weight (or solution-weight (matching-weight (first solutions))))
(solutions-provided? (not (null (first solutions)))))
`(deftest ,test-name ()
,@(when docstring (list docstring))
,@(when decls decls)
Expand Down Expand Up @@ -222,20 +233,18 @@ NOTE: This macro automatically rescales the pairs in `COORDINATES' to reside at
:timeout ,timeout
:timestep ,timestep)
(push time times)
(unless matching
(error "No matching produced."))
(unless (one-of matching
,@(loop :for correct-matching :in solutions
:collect `',correct-matching))
(unless (perfect-matching? matching ',coordinates)
(error "Found an imperfect matching: ~A" matching))
(when (< (matching-weight matching) ,solution-weight)
(error "Found better perfect matching: ~A has weight ~A<~A"
matching
(matching-weight matching)
,solution-weight))
(format t "~%Found perfect matching not in solutions: ~A~%"
matching))
(assert matching () "No matching produced.")
(assert (perfect-matching? matching ',coordinates) ()
"Found an imperfect matching: ~A" matching)
(assert (= (matching-weight matching) ,solution-weight) ()
"Found perfect matching w/ weight ~A != ~A (expected)."
(matching-weight matching) ,solution-weight)
(when ,solutions-provided?
(unless (one-of matching
,@(loop :for correct-matching :in solutions
:collect `',correct-matching))
(format t "~%Found perfect matching not in solutions: ~A~%"
matching)))
(is (= ,solution-weight (matching-weight matching)))))))
:finally
;; print out run-time statistics
Expand Down
66 changes: 66 additions & 0 deletions tests/cases.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
;;;; tests/cases.lisp
;;;;
;;;; Runs the distributed blossom algorithm against a family of graphs with known
;;;; MWPM, and asserts that we find the correct answer.

(in-package :anatevka-tests)

(eval-when (:compile-toplevel :load-toplevel :execute) ; needed at compile-time

(defun test-case-directory-filepath (dirname)
"Return the absolute pathname for `DIRNAME', which is the name of a test case directory in the `tests/py/cases'."
(declare (string dirname))
(let* ((anatevka-dir (ql:where-is-system "anatevka-tests")))
(merge-pathnames (concatenate 'string "tests/py/cases/" dirname "/*.*")
anatevka-dir)))

(defun parse-test-case-file (filename)
"Parses a test case file and extracts the MWPM value and the list of nodes."
(with-open-file (stream filename :direction :input)
(let ((mwpm-value (parse-integer (read-line stream))) ; read first line for mwpm
(nodes '()))
(loop :for line := (read-line stream nil)
:while line
:do (destructuring-bind (x y)
(mapcar #'parse-integer (uiop:split-string line :separator ","))
(push (list x y) nodes)))
(values mwpm-value (reverse nodes)))))

(defun process-test-case-directory (dirname)
"Iterates through all files in test case directory named `DIRNAME' and parses them."
(let* ((dirpath (test-case-directory-filepath dirname))
(files (directory dirpath)))
(loop :for file :in files
:collect (list file (multiple-value-list (parse-test-case-file file)))
:into test-cases
:finally (return test-cases))))

(defun sanitize-test-name (dirname test-path)
"Generates a valid Lisp symbol for a test name based on directory and file name."
(let* ((test-name (pathname-name test-path))
(clean-name (format nil "test-blossom-suite-~A-~A" dirname test-name)))
(intern (string-upcase (substitute #\- #\/ clean-name)) :anatevka-tests))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly this seems fine, since there aren't a bajillion test cases, but I have a dull memory that it's ill-advised to intern programmatically generated symbols. As an alternative, I guess we could just iterate through the directory at eval time of a single umbrella test, whether than at compile time to generate individuated tests?

Don't worry about it, just a comment.


(defmacro define-blossom-suite (dirname
(&key (border +default-border+)
(debug? nil)
(dryad-clock-rate +default-dryad-clock-rate+)
(iterations +default-iterations+)
(timeout +default-timeout+)
(timestep +default-timestep+)
(dryad-class 'dryad)))
"Creates a suite of `DEFINE-BLOSSOM-TEST's by processing the test case files at `DIRNAME' and asserting that we produce the correct minimum-weight perfect matching (MWPM). The `DIRNAME' is a number n specifying a directory containing test cases of random complete graphs laid out on an nxn grid. The test names produced by `DEFINE-BLOSSOM-SUITE' are of the form `TEST-BLOSSOM-SUITE-n-p-i' where p is the 'node density' (i.e. what proportion of the grid locations have a node at them) and i is the test case number."
`(progn
,@(loop :for (test-path (mwpm-value nodes))
:in (process-test-case-directory dirname)
:for test-name := (sanitize-test-name dirname test-path)
:collect
`(define-blossom-test ,test-name ,nodes
(:border ,border :debug? ,debug? :dryad-clock-rate ,dryad-clock-rate
:iterations ,iterations :timeout ,timeout :timestep ,timestep
:dryad-class ,dryad-class :solution-weight ,mwpm-value)
()))))

(define-blossom-suite "10" ()) ; 10x10 grid tests

(define-blossom-suite "20" ()) ; 20x20 grid tests
60 changes: 60 additions & 0 deletions tests/py/GenerateTestCases.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "9c4066b9-4681-4652-9339-7546f9f8ab43",
"metadata": {},
"outputs": [],
"source": [
"from generate_test_cases import generate_sparse_complete_graph, find_min_weight_perfect_matching, visualize_graph, save_matching_to_file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8340f011-13ac-4327-9bce-72dc87c7ede6",
"metadata": {},
"outputs": [],
"source": [
"p = 0.4\n",
"n = 20"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b075b58e-6ca9-45cd-83c6-9377cd7c2a89",
"metadata": {},
"outputs": [],
"source": [
"for i in range(0, 10):\n",
" G, nodes = generate_sparse_complete_graph(n, p)\n",
" matching, mwpm_value = find_min_weight_perfect_matching(G)\n",
" filename = f\"cases/{n}/{p}-{i}.txt\"\n",
" save_matching_to_file(filename, mwpm_value, nodes)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
4 changes: 4 additions & 0 deletions tests/py/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Python code for generating anatevka test cases

Install with `poetry`, and then open the `GenerateTestCases.ipynb` notebook in
`jupyter lab` to create additional test cases in the `cases` directory.
11 changes: 11 additions & 0 deletions tests/py/cases/10/0.1-0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
15
2,1
0,0
6,8
7,0
5,7
3,9
7,2
2,2
5,3
1,3
11 changes: 11 additions & 0 deletions tests/py/cases/10/0.1-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
14
8,8
2,4
9,0
2,1
7,7
4,3
1,4
1,7
5,3
2,8
11 changes: 11 additions & 0 deletions tests/py/cases/10/0.1-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
13
7,4
2,4
7,7
1,5
3,0
6,7
5,6
9,1
4,1
3,5
11 changes: 11 additions & 0 deletions tests/py/cases/10/0.1-3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
9
2,1
8,1
3,1
1,1
8,0
0,2
3,6
6,6
5,9
3,5
11 changes: 11 additions & 0 deletions tests/py/cases/10/0.1-4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
14
4,4
8,8
2,1
0,6
3,0
9,5
6,7
5,6
1,9
3,5
11 changes: 11 additions & 0 deletions tests/py/cases/10/0.1-5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
18
7,4
9,0
2,4
0,4
1,1
4,5
5,0
3,9
2,6
3,5
11 changes: 11 additions & 0 deletions tests/py/cases/10/0.1-6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
15
3,8
4,9
1,8
0,9
0,3
7,0
7,3
1,6
1,9
8,2
11 changes: 11 additions & 0 deletions tests/py/cases/10/0.1-7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
16
0,1
0,7
5,5
5,4
9,2
8,0
1,4
3,6
8,5
5,2
11 changes: 11 additions & 0 deletions tests/py/cases/10/0.1-8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
11
2,7
8,7
5,4
6,4
2,9
0,2
1,7
0,8
7,8
2,8
11 changes: 11 additions & 0 deletions tests/py/cases/10/0.1-9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
14
4,4
2,1
8,7
7,0
4,2
2,3
4,5
8,9
3,6
3,2
21 changes: 21 additions & 0 deletions tests/py/cases/10/0.2-0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
18
4,3
3,1
5,7
8,0
0,8
1,9
7,1
4,2
4,8
3,6
2,7
3,2
4,4
9,0
3,8
0,6
2,9
6,0
7,5
7,8
21 changes: 21 additions & 0 deletions tests/py/cases/10/0.2-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
23
3,7
5,1
8,0
0,2
8,6
2,5
4,2
3,6
0,1
2,4
7,9
6,7
3,5
9,0
8,1
0,0
9,6
2,0
0,9
6,9
Loading