From 6da8863f4bca9d92677638d2f679ddfd50004c4e Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 24 Jun 2025 10:15:02 -0600 Subject: [PATCH 01/31] Run all non-performance tests in CI instead of regression tests --- .ci-scripts/build_and_test.sh | 2 +- .github/workflows/test.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.ci-scripts/build_and_test.sh b/.ci-scripts/build_and_test.sh index c56bf728..6f955f55 100755 --- a/.ci-scripts/build_and_test.sh +++ b/.ci-scripts/build_and_test.sh @@ -263,7 +263,7 @@ then timed_message "Run regression tests" mpi_exe=$(grep 'MPIEXEC_EXECUTABLE' "${hostconfig_path}" | cut -d'"' -f2 | sed 's/;/ /g') - pytest -v -s -m regression --mpi-exec="${mpi_exe}" + pytest -v -s -m "not performance" --mpi-exec="${mpi_exe}" timed_message "Quandary tests completed" fi diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d0f3cb9a..16d29f52 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -50,7 +50,7 @@ jobs: run: | spack env activate .spack_env python -m pip install -e . --prefer-binary - pytest -v -s tests/regression --mpi-opt="--oversubscribe" + pytest -v -s -m "not performance" --mpi-opt="--oversubscribe" - name: Push packages and update index env: From b009b390ab03cf39b69030ad7e842e2fdb1f75f6 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 24 Jun 2025 11:07:08 -0600 Subject: [PATCH 02/31] Move mpiexec fixture to a conftest.py --- tests/python/conftest.py | 12 ++++++++++++ tests/python/test_env_variable.py | 9 --------- 2 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 tests/python/conftest.py diff --git a/tests/python/conftest.py b/tests/python/conftest.py new file mode 100644 index 00000000..6c818bc5 --- /dev/null +++ b/tests/python/conftest.py @@ -0,0 +1,12 @@ +"""Shared pytest fixtures for Python tests.""" + +import pytest + + +@pytest.fixture +def mpi_exec(request): + """Get MPI executor from pytest option.""" + executor = request.config.getoption("--mpi-exec") + if executor != "mpirun": + return f"{executor} -n " + return "mpirun -np " diff --git a/tests/python/test_env_variable.py b/tests/python/test_env_variable.py index 36aceda1..1da08373 100644 --- a/tests/python/test_env_variable.py +++ b/tests/python/test_env_variable.py @@ -120,15 +120,6 @@ def test_file_as_base_directory(quandary, request, tmp_path, clean_env_var, mpi_ assert file_path in str(excinfo.value) -@pytest.fixture -def mpi_exec(request): - """Get MPI executor from pytest option.""" - executor = request.config.getoption("--mpi-exec") - if executor != "mpirun": - return f"{executor} -n " - return "mpirun -np " - - @pytest.fixture def cd_tmp_path(tmp_path): """Change to a temporary directory for the test and return afterward.""" From 687f25156e2d3b16d5a55b372510900cbf605600 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 24 Jun 2025 10:19:51 -0600 Subject: [PATCH 03/31] Add test for cnot example through the python interface --- tests/python/test_example_cnot.py | 164 ++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 tests/python/test_example_cnot.py diff --git a/tests/python/test_example_cnot.py b/tests/python/test_example_cnot.py new file mode 100644 index 00000000..a0dd678b --- /dev/null +++ b/tests/python/test_example_cnot.py @@ -0,0 +1,164 @@ +import pytest +import numpy as np +from quandary import Quandary + +# Mark all tests in this file as regression tests +pytestmark = pytest.mark.regression + +REL_TOL = 1.0e-7 +ABS_TOL = 1.0e-15 + +# Sample every 100 time steps +SAMPLE_STEP = 100 +SAMPLE_INDICES = list(range(0, 1000, SAMPLE_STEP)) + +EXPECTED_PT = [ + [ + 3.9658691461922495, 1.10541078504851, 2.5534752325083, -5.01459964111086, -7.989518198565721, + 19.8647450324926, -11.67803451432, 9.83064584558409, -4.38133012164486, 5.30730946469264 + ], + [ + 31.2400305710471, -66.08853683466239, -0.465091455341277, 63.7248265188924, -21.174310603939798, + -79.05665721270739, 93.1340069390719, -46.9030952418057, 69.6982899074631, -75.945333755 + ], +] + +EXPECTED_QT = [ + [ + 9.862252388043519, -11.974045135091401, 6.59606355642564, 0.470220730556019, -6.96238968454898, + 1.44683564000136, 1.3431283726069598, 7.88787484285384, -10.6057026529729, 4.61727211900083 + ], + [ + -24.7819518399261, 84.1878324773596, -57.286612404866005, 60.7002321574017, -81.5873538588084, + 24.5564679603065, 70.41021934650581, -24.027297723508998, -69.5540861506332, 58.1373379857519 + ], +] + +EXPECTED_ENERGY = [ + [ + [ + 0.0, 0.0443127685743886, 0.503207525241488, 0.707485513252159, 0.92190965075134, + 0.487452233057886, 0.0659189040310057, 0.404806229139373, 0.621700867282782, 0.418405398025964 + ], + [ + 0.0, 0.0427783016984607, 0.405600998566902, 0.72910738824454, 0.913321148985495, + 0.501596986810575, 0.117409347015588, 0.295738092810018, 0.62534755580572, 0.391835768771235 + ], + [ + 1.0, 0.952870578107849, 0.51330421156213, 0.231076804756971, 0.0469090316748128, + 0.523616466963615, 0.902254320190267, 0.715768042541915, 0.374775615666352, 0.609611191500487 + ], + [ + 1.0, 0.960038351404126, 0.577887263952844, 0.332330293674496, 0.117860168444146, + 0.487334313786966, 0.914417429110783, 0.58368763524068, 0.378175961322588, 0.580147642107814 + ], + ], + [ + [ + 0.0, 0.114473223136999, 0.89365412503045, 0.125033950995173, 0.169036787529785, + 0.720428927135866, 0.976681894430636, 0.779086542498037, 0.969638523760001, 0.806602690681456 + ], + [ + 1.0, 0.880738445400608, 0.111681881442643, 0.896846315383814, 0.83845895823167, + 0.28684021932136, 0.0301558855495244, 0.22954869411915, 0.026841345557848, 0.19354082356098 + ], + [ + 0.0, 0.234235386928253, 0.625339960381513, 0.447433990734819, 0.177530699064567, + 0.0793832709583276, 0.373058049086058, 0.0378220594045343, 0.0605571768789592, 0.114455321435803 + ], + [ + 1.0, 0.770552944580204, 0.369324033000151, 0.530685742874723, 0.814973555117265, + 0.913347582445915, 0.620104170694281, 0.953542703675033, 0.942962953633296, 0.885401164374174 + ], + ], +] + +EXPECTED_POPULATION = [ + [ + [ + 1.0, 0.955687231426092, 0.496792474760431, 0.292514486752091, 0.0780903492531091, + 0.512547766947759, 0.934081095976383, 0.595193770868252, 0.378299132726335, 0.581594601984949 + ], + [ + 1.0, 0.957221698302315, 0.594399001435299, 0.270892611759853, 0.0866788510192543, + 0.498403013196075, 0.88259065299362, 0.704261907199483, 0.374652444205939, 0.608164231242167 + ], + [ + 0.0, 0.0471294218930485, 0.486695788440728, 0.768923195249066, 0.953090968331444, + 0.476383533045079, 0.0977456798208405, 0.284231957469638, 0.625224384347882, 0.390388808515223 + ], + [ + 0.0, 0.0399616485968552, 0.422112736049773, 0.667669706330358, 0.882139831561034, + 0.512665686222864, 0.0855825709022572, 0.416312364772957, 0.621824038693774, 0.419852357910149 + ], + ], + [ + [ + 1.0, 0.885526776863482, 0.106345874971468, 0.874966049009077, 0.830963212474665, + 0.279571072869779, 0.0233181055767533, 0.220913457509588, 0.0303614762491157, 0.193397309329457 + ], + [ + 0.0, 0.119261554600168, 0.888318118559558, 0.103153684620578, 0.16154104177308, + 0.713159780685289, 0.969844114459683, 0.77045130589035, 0.973158654453811, 0.806459176452422 + ], + [ + 1.0, 0.765764613072644, 0.374660039621344, 0.552566009271218, 0.822469300941689, + 0.920616729050366, 0.626941950925049, 0.962177940607019, 0.939442823135274, 0.885544678579907 + ], + [ + 0.0, 0.229447055420778, 0.630675967002465, 0.469314257130131, 0.185026444887915, + 0.086652417563915, 0.379895829318759, 0.0464572963386036, 0.0570370463830656, 0.114598835643788 + ], + ], +] + + +def test_example_cnot(mpi_exec): + """Test CNOT gate optimization using Python interface.""" + + freq01 = [4.80595, 4.8601] + favg = sum(freq01)/len(freq01) + rotfreq = favg*np.ones(len(freq01)) + unitary = np.identity(4) + unitary[2, 2] = 0.0 + unitary[3, 3] = 0.0 + unitary[2, 3] = 1.0 + unitary[3, 2] = 1.0 + n_osc = 2 + n_levels = 4 + + quandary = Quandary( + freq01=freq01, + Jkl=[0.005], + rotfreq=rotfreq, + T=200.0, + targetgate=unitary, + verbose=False, + randomize_init_ctrl=False, + nsteps=1000, + dT=0.2, + ) + + t, pt, qt, infidelity, expectedEnergy, population = quandary.optimize( + mpi_exec=mpi_exec, + maxcores=2 + ) + + assert t[0] == 0.0 and t[-1] == 200.0 + assert infidelity < 1e-4 + + for i in range(n_osc): + pt_samples = [pt[i][idx] for idx in SAMPLE_INDICES] + qt_samples = [qt[i][idx] for idx in SAMPLE_INDICES] + assert np.allclose(pt_samples, EXPECTED_PT[i], rtol=REL_TOL, atol=ABS_TOL) + assert np.allclose(qt_samples, EXPECTED_QT[i], rtol=REL_TOL, atol=ABS_TOL) + + for i in range(n_osc): + for j in range(n_levels): + energy_data = expectedEnergy[i][j] + energy_samples = [energy_data[idx] for idx in SAMPLE_INDICES] + assert np.allclose(energy_samples, EXPECTED_ENERGY[i][j], rtol=REL_TOL, atol=ABS_TOL) + + pop_data = population[i][j] + pop_samples = [pop_data[0, idx] for idx in SAMPLE_INDICES] + assert np.allclose(pop_samples, EXPECTED_POPULATION[i][j], rtol=REL_TOL, atol=ABS_TOL) From 9b9d0d92f4df103ea1064dca53a2a35e5e96ec64 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 24 Jun 2025 17:04:37 -0600 Subject: [PATCH 04/31] Use more informative array assert --- tests/python/test_example_cnot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/python/test_example_cnot.py b/tests/python/test_example_cnot.py index a0dd678b..8d08699a 100644 --- a/tests/python/test_example_cnot.py +++ b/tests/python/test_example_cnot.py @@ -150,15 +150,15 @@ def test_example_cnot(mpi_exec): for i in range(n_osc): pt_samples = [pt[i][idx] for idx in SAMPLE_INDICES] qt_samples = [qt[i][idx] for idx in SAMPLE_INDICES] - assert np.allclose(pt_samples, EXPECTED_PT[i], rtol=REL_TOL, atol=ABS_TOL) - assert np.allclose(qt_samples, EXPECTED_QT[i], rtol=REL_TOL, atol=ABS_TOL) + np.testing.assert_allclose(pt_samples, EXPECTED_PT[i], rtol=REL_TOL, atol=ABS_TOL) + np.testing.assert_allclose(qt_samples, EXPECTED_QT[i], rtol=REL_TOL, atol=ABS_TOL) for i in range(n_osc): for j in range(n_levels): energy_data = expectedEnergy[i][j] energy_samples = [energy_data[idx] for idx in SAMPLE_INDICES] - assert np.allclose(energy_samples, EXPECTED_ENERGY[i][j], rtol=REL_TOL, atol=ABS_TOL) + np.testing.assert_allclose(energy_samples, EXPECTED_ENERGY[i][j], rtol=REL_TOL, atol=ABS_TOL) pop_data = population[i][j] pop_samples = [pop_data[0, idx] for idx in SAMPLE_INDICES] - assert np.allclose(pop_samples, EXPECTED_POPULATION[i][j], rtol=REL_TOL, atol=ABS_TOL) + np.testing.assert_allclose(pop_samples, EXPECTED_POPULATION[i][j], rtol=REL_TOL, atol=ABS_TOL) From 7f242f92b0e06f2c13608e8b2b2e8f8237e1cb67 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Wed, 25 Jun 2025 12:47:07 -0600 Subject: [PATCH 05/31] Ignore spack folders when running pytests --- .pytest.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pytest.ini b/.pytest.ini index 6028462d..e7d21899 100644 --- a/.pytest.ini +++ b/.pytest.ini @@ -1,5 +1,5 @@ [pytest] -addopts = --ignore=blt --ignore=uberenv_libs +addopts = --ignore=blt --ignore=uberenv_libs --ignore=.spack_env --ignore=spack markers = regression: mark a test as a regression test From e89085553c83c8e02e111741b5ee21cc5c6b5d7b Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Wed, 25 Jun 2025 12:47:25 -0600 Subject: [PATCH 06/31] Increase test tolerances --- tests/python/test_example_cnot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/python/test_example_cnot.py b/tests/python/test_example_cnot.py index 8d08699a..ef28cdab 100644 --- a/tests/python/test_example_cnot.py +++ b/tests/python/test_example_cnot.py @@ -5,8 +5,8 @@ # Mark all tests in this file as regression tests pytestmark = pytest.mark.regression -REL_TOL = 1.0e-7 -ABS_TOL = 1.0e-15 +REL_TOL = 1.0e-3 +ABS_TOL = 1.0e-10 # Sample every 100 time steps SAMPLE_STEP = 100 From 30a61980365613366c02362e0597be26338d2e4c Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Mon, 30 Jun 2025 14:44:38 -0600 Subject: [PATCH 07/31] Compare infidelity to expected value --- tests/python/test_example_cnot.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/python/test_example_cnot.py b/tests/python/test_example_cnot.py index ef28cdab..05960dd2 100644 --- a/tests/python/test_example_cnot.py +++ b/tests/python/test_example_cnot.py @@ -1,5 +1,6 @@ import pytest import numpy as np +from pytest import approx from quandary import Quandary # Mark all tests in this file as regression tests @@ -12,6 +13,8 @@ SAMPLE_STEP = 100 SAMPLE_INDICES = list(range(0, 1000, SAMPLE_STEP)) +EXPECTED_INFIDELITY = 7.059941000597814e-05 + EXPECTED_PT = [ [ 3.9658691461922495, 1.10541078504851, 2.5534752325083, -5.01459964111086, -7.989518198565721, @@ -145,7 +148,7 @@ def test_example_cnot(mpi_exec): ) assert t[0] == 0.0 and t[-1] == 200.0 - assert infidelity < 1e-4 + assert infidelity == approx(EXPECTED_INFIDELITY, rel=REL_TOL, abs=ABS_TOL) for i in range(n_osc): pt_samples = [pt[i][idx] for idx in SAMPLE_INDICES] From b917c4a25b6d0fc69151f306bebc2b4c658e42ee Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Mon, 30 Jun 2025 15:07:13 -0600 Subject: [PATCH 08/31] Remove nsteps and dT and update expected results --- tests/python/test_example_cnot.py | 85 +++++++++++++++---------------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/tests/python/test_example_cnot.py b/tests/python/test_example_cnot.py index 05960dd2..ee06281a 100644 --- a/tests/python/test_example_cnot.py +++ b/tests/python/test_example_cnot.py @@ -13,65 +13,65 @@ SAMPLE_STEP = 100 SAMPLE_INDICES = list(range(0, 1000, SAMPLE_STEP)) -EXPECTED_INFIDELITY = 7.059941000597814e-05 +EXPECTED_INFIDELITY = 9.579442993001841e-05 EXPECTED_PT = [ [ - 3.9658691461922495, 1.10541078504851, 2.5534752325083, -5.01459964111086, -7.989518198565721, - 19.8647450324926, -11.67803451432, 9.83064584558409, -4.38133012164486, 5.30730946469264 + 6.616725193332, -2.69000519001337, 7.37277813386157, -3.74776129300963, -6.84027288145787, + -11.3843214681583, 22.9942959921893, -12.5735054683736, 16.340616616697098, 5.03169262640028 ], [ - 31.2400305710471, -66.08853683466239, -0.465091455341277, 63.7248265188924, -21.174310603939798, - -79.05665721270739, 93.1340069390719, -46.9030952418057, 69.6982899074631, -75.945333755 + 41.115114703615404, 6.20339346604496, -57.143070676365696, 47.8892790887547, -14.8666376642723, + 12.2136130909702, -100.26207561761501, 162.83351141781998, -75.9622207062174, -72.1202107523129 ], ] EXPECTED_QT = [ [ - 9.862252388043519, -11.974045135091401, 6.59606355642564, 0.470220730556019, -6.96238968454898, - 1.44683564000136, 1.3431283726069598, 7.88787484285384, -10.6057026529729, 4.61727211900083 + 12.2046682803128, -8.079881699053109, 16.4875320760023, -5.461025628965721, 11.1234357226862, + -12.7826810046159, 0.147440839329306, 22.7946487720523, -10.3817628748433, -7.76762358999419 ], [ - -24.7819518399261, 84.1878324773596, -57.286612404866005, 60.7002321574017, -81.5873538588084, - 24.5564679603065, 70.41021934650581, -24.027297723508998, -69.5540861506332, 58.1373379857519 + -42.4224419634531, 154.38104118826502, -106.09535149239501, -47.3984982589973, 137.734223043753, + -126.00428091818799, 63.2898514048737, -31.3690850739396, 78.0954617941575, -64.22668926201 ], ] EXPECTED_ENERGY = [ [ [ - 0.0, 0.0443127685743886, 0.503207525241488, 0.707485513252159, 0.92190965075134, - 0.487452233057886, 0.0659189040310057, 0.404806229139373, 0.621700867282782, 0.418405398025964 + 0.0, 0.0343776867606554, 0.526796763769277, 0.780954240543288, 0.866125816361883, + 0.960524117458703, 0.357409349352425, 0.0855357085561291, 0.0452846026271667, 0.521501498149395 ], [ - 0.0, 0.0427783016984607, 0.405600998566902, 0.72910738824454, 0.913321148985495, - 0.501596986810575, 0.117409347015588, 0.295738092810018, 0.62534755580572, 0.391835768771235 + 0.0, 0.0469063275861075, 0.509147153889098, 0.756960458910116, 0.839965243568155, + 0.967039095793251, 0.281757944150766, 0.0875800293040959, 0.0333098058877516, 0.359796138200774 ], [ - 1.0, 0.952870578107849, 0.51330421156213, 0.231076804756971, 0.0469090316748128, - 0.523616466963615, 0.902254320190267, 0.715768042541915, 0.374775615666352, 0.609611191500487 + 1.0, 0.965563423724401, 0.467777024255108, 0.199218651155181, 0.124914718210749, + 0.0424256184064999, 0.73355546218628, 0.923072091142331, 0.975849082136768, 0.64319313982527 ], [ - 1.0, 0.960038351404126, 0.577887263952844, 0.332330293674496, 0.117860168444146, - 0.487334313786966, 0.914417429110783, 0.58368763524068, 0.378175961322588, 0.580147642107814 + 1.0, 0.953152562102101, 0.496279057642638, 0.262866649149181, 0.168994221944719, + 0.0300111684909359, 0.627277244373336, 0.90381217136351, 0.945556509484767, 0.475509224112312 ], ], [ [ - 0.0, 0.114473223136999, 0.89365412503045, 0.125033950995173, 0.169036787529785, - 0.720428927135866, 0.976681894430636, 0.779086542498037, 0.969638523760001, 0.806602690681456 + 0.0, 0.831977237672821, 0.191357346184574, 0.625910716277095, 0.937379902798678, + 0.00963598916620533, 0.179106523427556, 0.670087999937763, 0.763978679766654, 0.0544493184467886 ], [ - 1.0, 0.880738445400608, 0.111681881442643, 0.896846315383814, 0.83845895823167, - 0.28684021932136, 0.0301558855495244, 0.22954869411915, 0.026841345557848, 0.19354082356098 + 1.0, 0.167523903453768, 0.811130164498519, 0.361501635864612, 0.0500738912924525, + 0.996953036235878, 0.813633790190331, 0.322484955601581, 0.236119318537149, 0.942765139557318 ], [ - 0.0, 0.234235386928253, 0.625339960381513, 0.447433990734819, 0.177530699064567, - 0.0793832709583276, 0.373058049086058, 0.0378220594045343, 0.0605571768789592, 0.114455321435803 + 0.0, 0.722645324846759, 0.381156069703124, 0.27076601420254, 0.587949469059606, + 0.306348721091516, 0.638755583149189, 0.939883877428134, 0.742650628373161, 0.829280585662997 ], [ - 1.0, 0.770552944580204, 0.369324033000151, 0.530685742874723, 0.814973555117265, - 0.913347582445915, 0.620104170694281, 0.953542703675033, 0.942962953633296, 0.885401164374174 + 1.0, 0.277853534003518, 0.616356419650465, 0.741821633765665, 0.424596736918113, + 0.687062253461252, 0.368504103524597, 0.0675431672219572, 0.257251373411591, 0.173504956452293 ], ], ] @@ -79,38 +79,38 @@ EXPECTED_POPULATION = [ [ [ - 1.0, 0.955687231426092, 0.496792474760431, 0.292514486752091, 0.0780903492531091, - 0.512547766947759, 0.934081095976383, 0.595193770868252, 0.378299132726335, 0.581594601984949 + 1.0, 0.96562231323944, 0.473203236234554, 0.219045759462923, 0.133874183644436, + 0.0394758825489966, 0.642590650656231, 0.914464291453796, 0.954715397383493, 0.478498501862085 ], [ - 1.0, 0.957221698302315, 0.594399001435299, 0.270892611759853, 0.0866788510192543, - 0.498403013196075, 0.88259065299362, 0.704261907199483, 0.374652444205939, 0.608164231242167 + 1.0, 0.953093672413988, 0.490852846114757, 0.243039541097649, 0.160034756439763, + 0.0329609042160594, 0.718242055858744, 0.912419970707285, 0.966690194125127, 0.640203861813051 ], [ - 0.0, 0.0471294218930485, 0.486695788440728, 0.768923195249066, 0.953090968331444, - 0.476383533045079, 0.0977456798208405, 0.284231957469638, 0.625224384347882, 0.390388808515223 + 0.0, 0.0344365762757087, 0.532222975748686, 0.800781348850533, 0.875085281795089, + 0.957574381600628, 0.266444537821086, 0.0769279088670572, 0.0241509178738421, 0.356806860185867 ], [ - 0.0, 0.0399616485968552, 0.422112736049773, 0.667669706330358, 0.882139831561034, - 0.512665686222864, 0.0855825709022572, 0.416312364772957, 0.621824038693774, 0.419852357910149 + 0.0, 0.0468474378979889, 0.503720942360812, 0.737133350856038, 0.83100577806066, + 0.969988831515816, 0.372722755633689, 0.0961878286455842, 0.0544434905259422, 0.524490775899009 ], ], [ [ - 1.0, 0.885526776863482, 0.106345874971468, 0.874966049009077, 0.830963212474665, - 0.279571072869779, 0.0233181055767533, 0.220913457509588, 0.0303614762491157, 0.193397309329457 + 1.0, 0.168022762327275, 0.808642653819256, 0.374089283729116, 0.0626200972076404, + 0.990364010841494, 0.8208934765811, 0.329912000072162, 0.236021320244006, 0.945550681564691 ], [ - 0.0, 0.119261554600168, 0.888318118559558, 0.103153684620578, 0.16154104177308, - 0.713159780685289, 0.969844114459683, 0.77045130589035, 0.973158654453811, 0.806459176452422 + 0.0, 0.832476096546328, 0.188869835505336, 0.638498364143153, 0.949926108715465, + 0.00304696377343223, 0.186366209819179, 0.6775150444098, 0.76388068147573, 0.0572348604565071 ], [ - 1.0, 0.765764613072644, 0.374660039621344, 0.552566009271218, 0.822469300941689, - 0.920616729050366, 0.626941950925049, 0.962177940607019, 0.939442823135274, 0.885544678579907 + 1.0, 0.277354675153351, 0.61884393030067, 0.729233985803174, 0.412050530946231, + 0.693651278915612, 0.361244416858177, 0.0601161225812547, 0.25734937163745, 0.17071941434814 ], [ - 0.0, 0.229447055420778, 0.630675967002465, 0.469314257130131, 0.185026444887915, - 0.086652417563915, 0.379895829318759, 0.0464572963386036, 0.0570370463830656, 0.114598835643788 + 0.0, 0.722146465996572, 0.383643580352985, 0.258178366239553, 0.575403263087266, + 0.312937746545499, 0.631495896482428, 0.932456832787137, 0.742748626599118, 0.826495043559029 ], ], ] @@ -138,8 +138,6 @@ def test_example_cnot(mpi_exec): targetgate=unitary, verbose=False, randomize_init_ctrl=False, - nsteps=1000, - dT=0.2, ) t, pt, qt, infidelity, expectedEnergy, population = quandary.optimize( @@ -148,6 +146,7 @@ def test_example_cnot(mpi_exec): ) assert t[0] == 0.0 and t[-1] == 200.0 + assert len(t) == 1222 assert infidelity == approx(EXPECTED_INFIDELITY, rel=REL_TOL, abs=ABS_TOL) for i in range(n_osc): From 69ef06f1ee11a5f3455c8d3cdadf5b4274a26535 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Mon, 30 Jun 2025 17:04:46 -0600 Subject: [PATCH 09/31] Update sampling of test data and small cleanup --- tests/python/test_example_cnot.py | 101 +++++++++++++++--------------- 1 file changed, 52 insertions(+), 49 deletions(-) diff --git a/tests/python/test_example_cnot.py b/tests/python/test_example_cnot.py index ee06281a..4ef0d29c 100644 --- a/tests/python/test_example_cnot.py +++ b/tests/python/test_example_cnot.py @@ -9,69 +9,66 @@ REL_TOL = 1.0e-3 ABS_TOL = 1.0e-10 -# Sample every 100 time steps -SAMPLE_STEP = 100 -SAMPLE_INDICES = list(range(0, 1000, SAMPLE_STEP)) - +EXPECTED_LENGTH = 1222 EXPECTED_INFIDELITY = 9.579442993001841e-05 EXPECTED_PT = [ [ - 6.616725193332, -2.69000519001337, 7.37277813386157, -3.74776129300963, -6.84027288145787, - -11.3843214681583, 22.9942959921893, -12.5735054683736, 16.340616616697098, 5.03169262640028 + 6.616725193332, 0.594108955114234, -4.6612041043857, -10.057008545847399, -16.7925272532101, + -4.37803267051435, 29.429289693884897, -1.99065216688873, 1.55672833280728, -8.84077714799125 ], [ - 41.115114703615404, 6.20339346604496, -57.143070676365696, 47.8892790887547, -14.8666376642723, - 12.2136130909702, -100.26207561761501, 162.83351141781998, -75.9622207062174, -72.1202107523129 + 41.115114703615404, -126.979851925522, 1.95887435527856, -44.1884354195065, 69.1903244908076, + 98.6254243074309, -72.6834065507498, -7.877713819489899, -93.5283459543858, 12.9802269197434 ], ] EXPECTED_QT = [ [ - 12.2046682803128, -8.079881699053109, 16.4875320760023, -5.461025628965721, 11.1234357226862, - -12.7826810046159, 0.147440839329306, 22.7946487720523, -10.3817628748433, -7.76762358999419 + 12.2046682803128, -4.106223161934349, -4.25224002148494, 14.913352257247501, -10.267172008817, + 12.213110218227799, -7.2090372842489, -27.3356430221432, -0.0754141037646605, -14.1999385354707 ], [ - -42.4224419634531, 154.38104118826502, -106.09535149239501, -47.3984982589973, 137.734223043753, - -126.00428091818799, 63.2898514048737, -31.3690850739396, 78.0954617941575, -64.22668926201 + -42.4224419634531, 70.5220436508868, -55.3237840325884, 138.403062344659, -27.9645427362594, + -109.78387954138701, 41.9326700775612, -105.26165922349901, 115.179149640371, -3.45010594052695 ], ] EXPECTED_ENERGY = [ [ [ - 0.0, 0.0343776867606554, 0.526796763769277, 0.780954240543288, 0.866125816361883, - 0.960524117458703, 0.357409349352425, 0.0855357085561291, 0.0452846026271667, 0.521501498149395 + 0.0, 0.164018720327432, 0.681155470507073, 0.91595079897646, 0.409625193180763, + 0.0451618509044243, 0.145909796953593, 0.894811107898173, 0.276174785115582, 7.18972153758039e-05 ], [ - 0.0, 0.0469063275861075, 0.509147153889098, 0.756960458910116, 0.839965243568155, - 0.967039095793251, 0.281757944150766, 0.0875800293040959, 0.0333098058877516, 0.359796138200774 + 0.0, 0.148554713902497, 0.677563453874268, 0.883069457661089, 0.433062321962301, + 0.0409323679823027, 0.237968075668888, 0.937615058415591, 0.288744113780608, 6.80597196600991e-05 ], [ - 1.0, 0.965563423724401, 0.467777024255108, 0.199218651155181, 0.124914718210749, - 0.0424256184064999, 0.73355546218628, 0.923072091142331, 0.975849082136768, 0.64319313982527 + 1.0, 0.833693887189515, 0.30231425655866, 0.0781041328616079, 0.612526687583752, + 0.950165031479551, 0.785949803569589, 0.0630467958468525, 0.713636929875362, 0.9999200364916 ], [ - 1.0, 0.953152562102101, 0.496279057642638, 0.262866649149181, 0.168994221944719, - 0.0300111684909359, 0.627277244373336, 0.90381217136351, 0.945556509484767, 0.475509224112312 + 1.0, 0.853732678324426, 0.338966818414533, 0.122875610552277, 0.54478579702393, + 0.963740749531197, 0.830172323495192, 0.104527037572075, 0.7214441710184, 0.99994000664992 ], ], [ [ - 0.0, 0.831977237672821, 0.191357346184574, 0.625910716277095, 0.937379902798678, - 0.00963598916620533, 0.179106523427556, 0.670087999937763, 0.763978679766654, 0.0544493184467886 + 0.0, 0.833380867552618, 0.211467502291388, 0.10772688588061, 0.994486567753026, + 0.332783402008406, 0.328512418616942, 0.849318770276658, 0.365928160207867, 6.3927010622781e-05 ], [ - 1.0, 0.167523903453768, 0.811130164498519, 0.361501635864612, 0.0500738912924525, - 0.996953036235878, 0.813633790190331, 0.322484955601581, 0.236119318537149, 0.942765139557318 + 1.0, 0.165804605873793, 0.794830994785911, 0.891785033888527, 0.00521798681585307, + 0.658576469218537, 0.682364479561092, 0.154976310427688, 0.637505155319165, 0.999947805542938 ], [ - 0.0, 0.722645324846759, 0.381156069703124, 0.27076601420254, 0.587949469059606, - 0.306348721091516, 0.638755583149189, 0.939883877428134, 0.742650628373161, 0.829280585662997 + 0.0, 0.926141799780157, 0.636879769305457, 0.0297187946675154, 0.576532188630711, + 0.937277826669486, 0.1423287253708, 0.0620880380451264, 0.477483384114772, 0.999939141264031 ], [ - 1.0, 0.277853534003518, 0.616356419650465, 0.741821633765665, 0.424596736918113, - 0.687062253461252, 0.368504103524597, 0.0675431672219572, 0.257251373411591, 0.173504956452293 + 1.0, 0.0746727268238372, 0.356821733548592, 0.970769285553542, 0.423763256939225, + 0.0713623024323497, 0.846794376113813, 0.933616880573983, 0.519083300077874, 4.91262629975248e-05 ], ], ] @@ -79,42 +76,46 @@ EXPECTED_POPULATION = [ [ [ - 1.0, 0.96562231323944, 0.473203236234554, 0.219045759462923, 0.133874183644436, - 0.0394758825489966, 0.642590650656231, 0.914464291453796, 0.954715397383493, 0.478498501862085 + 1.0, 0.835981279673114, 0.318844529497196, 0.0840492010301489, 0.590374806827084, + 0.954838149104735, 0.854090203057394, 0.105188892114705, 0.723825214898353, 0.999928102824303 ], [ - 1.0, 0.953093672413988, 0.490852846114757, 0.243039541097649, 0.160034756439763, - 0.0329609042160594, 0.718242055858744, 0.912419970707285, 0.966690194125127, 0.640203861813051 + 1.0, 0.851445286098087, 0.322436546129973, 0.116930542347156, 0.566937678047111, + 0.959067632027563, 0.762031924344571, 0.0623849415993417, 0.71125588623519, 0.999931940321825 ], [ - 0.0, 0.0344365762757087, 0.532222975748686, 0.800781348850533, 0.875085281795089, - 0.957574381600628, 0.266444537821086, 0.0769279088670572, 0.0241509178738421, 0.356806860185867 + 0.0, 0.166306112811131, 0.697685743445493, 0.921895867144501, 0.387473312423499, + 0.0498349685284324, 0.214050196441385, 0.936953204165973, 0.286363070138658, 7.99635494829092e-05 ], [ - 0.0, 0.0468474378979889, 0.503720942360812, 0.737133350856038, 0.83100577806066, - 0.969988831515816, 0.372722755633689, 0.0961878286455842, 0.0544434905259422, 0.524490775899009 + 0.0, 0.146267321676212, 0.661033181589251, 0.87712438945341, 0.455214202982917, + 0.0362592504767486, 0.169827676515773, 0.89547296244067, 0.278555828995427, 5.99933849104876e-05 ], ], [ [ - 1.0, 0.168022762327275, 0.808642653819256, 0.374089283729116, 0.0626200972076404, - 0.990364010841494, 0.8208934765811, 0.329912000072162, 0.236021320244006, 0.945550681564691 + 1.0, 0.166619132447928, 0.788532497712881, 0.892273114125999, 0.00551343225482061, + 0.667216598000753, 0.671487581394045, 0.15068122973622, 0.634071839806068, 0.999936073029056 ], [ - 0.0, 0.832476096546328, 0.188869835505336, 0.638498364143153, 0.949926108715465, - 0.00304696377343223, 0.186366209819179, 0.6775150444098, 0.76388068147573, 0.0572348604565071 + 0.0, 0.834195394126791, 0.20516900521833, 0.108214966119718, 0.994782013193558, + 0.341423530791329, 0.317635520452367, 0.845023689587245, 0.362494844696633, 5.21944985474435e-05 ], [ - 1.0, 0.277354675153351, 0.61884393030067, 0.729233985803174, 0.412050530946231, - 0.693651278915612, 0.361244416858177, 0.0601161225812547, 0.25734937163745, 0.17071941434814 + 1.0, 0.0738582002204882, 0.363120230698696, 0.970281205338593, 0.423467811376541, + 0.0627221733384974, 0.857671274640174, 0.937911961967699, 0.522516615899248, 6.08587770516464e-05 ], [ - 0.0, 0.722146465996572, 0.383643580352985, 0.258178366239553, 0.575403263087266, - 0.312937746545499, 0.631495896482428, 0.932456832787137, 0.742748626599118, 0.826495043559029 + 0.0, 0.925327273176801, 0.643178266455191, 0.0292307144521444, 0.576236743067622, + 0.928637697575596, 0.153205623897152, 0.0663831194387622, 0.480916699935952, 0.999950873771833 ], ], ] +# Compare output to expected result for 10 points +NUM_SAMPLES = 10 +SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] + def test_example_cnot(mpi_exec): """Test CNOT gate optimization using Python interface.""" @@ -122,6 +123,8 @@ def test_example_cnot(mpi_exec): freq01 = [4.80595, 4.8601] favg = sum(freq01)/len(freq01) rotfreq = favg*np.ones(len(freq01)) + T = 200.0 + unitary = np.identity(4) unitary[2, 2] = 0.0 unitary[3, 3] = 0.0 @@ -134,19 +137,19 @@ def test_example_cnot(mpi_exec): freq01=freq01, Jkl=[0.005], rotfreq=rotfreq, - T=200.0, + T=T, targetgate=unitary, verbose=False, randomize_init_ctrl=False, ) - t, pt, qt, infidelity, expectedEnergy, population = quandary.optimize( + t, pt, qt, infidelity, energy, population = quandary.optimize( mpi_exec=mpi_exec, maxcores=2 ) - assert t[0] == 0.0 and t[-1] == 200.0 - assert len(t) == 1222 + assert t[0] == 0.0 and t[-1] == T + assert len(t) == EXPECTED_LENGTH assert infidelity == approx(EXPECTED_INFIDELITY, rel=REL_TOL, abs=ABS_TOL) for i in range(n_osc): @@ -157,7 +160,7 @@ def test_example_cnot(mpi_exec): for i in range(n_osc): for j in range(n_levels): - energy_data = expectedEnergy[i][j] + energy_data = energy[i][j] energy_samples = [energy_data[idx] for idx in SAMPLE_INDICES] np.testing.assert_allclose(energy_samples, EXPECTED_ENERGY[i][j], rtol=REL_TOL, atol=ABS_TOL) From 3a3210d94ab2e71769df2507484ad29a64da0b44 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Mon, 30 Jun 2025 17:47:11 -0600 Subject: [PATCH 10/31] Refactor test assert to helper function --- tests/python/test_example_cnot.py | 39 ++++++----- tests/python/utils.py | 108 ++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 20 deletions(-) create mode 100644 tests/python/utils.py diff --git a/tests/python/test_example_cnot.py b/tests/python/test_example_cnot.py index 4ef0d29c..8931ef28 100644 --- a/tests/python/test_example_cnot.py +++ b/tests/python/test_example_cnot.py @@ -1,7 +1,7 @@ import pytest import numpy as np -from pytest import approx from quandary import Quandary +from utils import assert_results_equal # Mark all tests in this file as regression tests pytestmark = pytest.mark.regression @@ -148,22 +148,21 @@ def test_example_cnot(mpi_exec): maxcores=2 ) - assert t[0] == 0.0 and t[-1] == T - assert len(t) == EXPECTED_LENGTH - assert infidelity == approx(EXPECTED_INFIDELITY, rel=REL_TOL, abs=ABS_TOL) - - for i in range(n_osc): - pt_samples = [pt[i][idx] for idx in SAMPLE_INDICES] - qt_samples = [qt[i][idx] for idx in SAMPLE_INDICES] - np.testing.assert_allclose(pt_samples, EXPECTED_PT[i], rtol=REL_TOL, atol=ABS_TOL) - np.testing.assert_allclose(qt_samples, EXPECTED_QT[i], rtol=REL_TOL, atol=ABS_TOL) - - for i in range(n_osc): - for j in range(n_levels): - energy_data = energy[i][j] - energy_samples = [energy_data[idx] for idx in SAMPLE_INDICES] - np.testing.assert_allclose(energy_samples, EXPECTED_ENERGY[i][j], rtol=REL_TOL, atol=ABS_TOL) - - pop_data = population[i][j] - pop_samples = [pop_data[0, idx] for idx in SAMPLE_INDICES] - np.testing.assert_allclose(pop_samples, EXPECTED_POPULATION[i][j], rtol=REL_TOL, atol=ABS_TOL) + assert_results_equal( + t=t, + pt=pt, + qt=qt, + infidelity=infidelity, + energy=energy, + population=population, + T=T, + n_osc=n_osc, + n_levels=n_levels, + expected_length=EXPECTED_LENGTH, + expected_infidelity=EXPECTED_INFIDELITY, + expected_pt=EXPECTED_PT, + expected_qt=EXPECTED_QT, + expected_energy=EXPECTED_ENERGY, + expected_population=EXPECTED_POPULATION, + sample_indices=SAMPLE_INDICES + ) diff --git a/tests/python/utils.py b/tests/python/utils.py new file mode 100644 index 00000000..c2163d5b --- /dev/null +++ b/tests/python/utils.py @@ -0,0 +1,108 @@ +import numpy as np +from pytest import approx + +REL_TOL = 1.0e-3 +ABS_TOL = 1.0e-10 + + +def assert_results_equal( + t, pt, qt, infidelity, energy, population, T, n_osc, n_levels, sample_indices, + expected_length, expected_infidelity, expected_pt, expected_qt, expected_energy, expected_population + ): + """ + Utility function to assert that the results of quantum run match the expected values within given tolerances. + """ + assert t[0] == 0.0 and t[-1] == T + assert len(t) == expected_length + assert infidelity == approx(expected_infidelity, rel=REL_TOL, abs=ABS_TOL) + + for i in range(n_osc): + pt_samples = [pt[i][idx] for idx in sample_indices] + qt_samples = [qt[i][idx] for idx in sample_indices] + np.testing.assert_allclose(pt_samples, expected_pt[i], rtol=REL_TOL, atol=ABS_TOL) + np.testing.assert_allclose(qt_samples, expected_qt[i], rtol=REL_TOL, atol=ABS_TOL) + + for i in range(n_osc): + for j in range(n_levels): + energy_data = energy[i][j] + energy_samples = [energy_data[idx] for idx in sample_indices] + np.testing.assert_allclose(energy_samples, expected_energy[i][j], rtol=REL_TOL, atol=ABS_TOL) + + pop_data = population[i][j] + pop_samples = [pop_data[0, idx] for idx in sample_indices] + np.testing.assert_allclose(pop_samples, expected_population[i][j], rtol=REL_TOL, atol=ABS_TOL) + + +def print_expected_values(infidelity, pt, qt, expectedEnergy, population, sample_indices, n_osc): + """ + Utility function to print actual values in the format needed for EXPECTED arrays. + Call this function with actual test results to get copy-pasteable expected values. + """ + print() + print(f"EXPECTED_LENGTH = {len(pt[0])}") + print(f"EXPECTED_INFIDELITY = {infidelity}") + print() + + print("EXPECTED_PT = [") + for i in range(n_osc): + pt_samples = [pt[i][idx] for idx in sample_indices] + print(" [") + # Format with max 5 values per line for readability + for j in range(0, len(pt_samples), 5): + chunk = pt_samples[j:j+5] + if j + 5 >= len(pt_samples): + print(f" {', '.join(map(str, chunk))}") + else: + print(f" {', '.join(map(str, chunk))},") + print(" ],") + print("]") + print() + + print("EXPECTED_QT = [") + for i in range(n_osc): + qt_samples = [qt[i][idx] for idx in sample_indices] + print(" [") + for j in range(0, len(qt_samples), 5): + chunk = qt_samples[j:j+5] + if j + 5 >= len(qt_samples): + print(f" {', '.join(map(str, chunk))}") + else: + print(f" {', '.join(map(str, chunk))},") + print(" ],") + print("]") + print() + + print("EXPECTED_ENERGY = [") + for i in range(n_osc): + print(" [") + for j in range(len(expectedEnergy[i])): + energy_data = expectedEnergy[i][j] + energy_samples = [energy_data[idx] for idx in sample_indices] + print(" [") + for k in range(0, len(energy_samples), 5): + chunk = energy_samples[k:k+5] + if k + 5 >= len(energy_samples): + print(f" {', '.join(map(str, chunk))}") + else: + print(f" {', '.join(map(str, chunk))},") + print(" ],") + print(" ],") + print("]") + print() + + print("EXPECTED_POPULATION = [") + for i in range(n_osc): + print(" [") + for j in range(len(population[i])): + pop_data = population[i][j] + pop_samples = [pop_data[0, idx] for idx in sample_indices] + print(" [") + for k in range(0, len(pop_samples), 5): + chunk = pop_samples[k:k+5] + if k + 5 >= len(pop_samples): + print(f" {', '.join(map(str, chunk))}") + else: + print(f" {', '.join(map(str, chunk))},") + print(" ],") + print(" ],") + print("]") From 051f49ac66b35fcbced169509baaafe4b4705eea Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 09:13:29 -0600 Subject: [PATCH 11/31] Add cnot with guard levels test --- .../test_example_cnot_withguardlevels.py | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 tests/python/test_example_cnot_withguardlevels.py diff --git a/tests/python/test_example_cnot_withguardlevels.py b/tests/python/test_example_cnot_withguardlevels.py new file mode 100644 index 00000000..b66be6b2 --- /dev/null +++ b/tests/python/test_example_cnot_withguardlevels.py @@ -0,0 +1,185 @@ +import pytest +import numpy as np +from quandary import Quandary +from utils import assert_results_equal + +# Mark all tests in this file as regression tests +pytestmark = pytest.mark.regression + +REL_TOL = 1.0e-3 +ABS_TOL = 1.0e-10 + +EXPECTED_LENGTH = 1806 +EXPECTED_INFIDELITY = 0.480188055481932 + +EXPECTED_PT = [ + [ + -3.89196260920326, -6.69814371177082, -9.20896335367253, -7.981320397746599, -3.36187997549283, + -4.6970770222456, -10.0164546119019, -11.2224622837805, -8.33354767429947, -4.95697334818543 + ], + [ + -6.48647374260631, -4.35103011119594, 7.081723515132199, 18.6998569666555, 26.3683265670305, + 24.8348573506472, 15.4646934377398, 3.37175363050847, -6.28742788663761, -7.36137743473769 + ], +] + +EXPECTED_QT = [ + [ + 12.1290294821262, 17.1562352925453, 16.841746061092298, 15.4699999874251, 10.259217466023001, + -8.83471122356989, -14.3105635348399, -16.3085121425574, -17.2025933654577, -12.1886379441658 + ], + [ + -10.1470331522652, -18.688440880792403, -23.635847402275, -22.136075762169398, -13.1942357679238, + 14.1015046555011, 22.4389342569253, 22.555547092136, 16.5938299318193, 8.48320408151263 + ], +] + +EXPECTED_ENERGY = [ + [ + [ + 0.0, 0.011870296095525, 0.0549129666577761, 0.121300531682623, 0.182155079950606, + 0.196249811103354, 0.15781831384911, 0.0960343416859211, 0.0396436169849094, 0.0129998178824294 + ], + [ + 0.0, 0.0129062587700882, 0.0570009187532015, 0.120028226159957, 0.168962621690027, + 0.166503578909807, 0.119440762207014, 0.0651696785233795, 0.0340064899053436, 0.038502126221005 + ], + [ + 1.0, 1.00604218340943, 0.984078386298889, 0.897698403495831, 0.813586980815862, + 0.793095365408016, 0.834523137010483, 0.905846679900428, 0.958608789131472, 0.971815385618592 + ], + [ + 1.0, 1.00663300051438, 0.982374995844373, 0.899642145587353, 0.841197229501374, + 0.852285120098703, 0.915301305879275, 0.991723517720418, 1.0179038097135, 0.991001018080102 + ], + ], + [ + [ + 0.0, 0.0123980424997909, 0.0696972089115495, 0.188836900438941, 0.344802858282004, + 0.481236657027539, 0.577666595359302, 0.619001419491198, 0.592135582199842, 0.545075828331161 + ], + [ + 1.0, 1.00635402942333, 0.979367467879334, 0.84191613237523, 0.661629449782256, + 0.535302136943995, 0.452220823513964, 0.397744391779343, 0.396143400841863, 0.433577603144192 + ], + [ + 0.0, 0.0132763546579357, 0.0689189602642812, 0.185722461571837, 0.361686415675913, + 0.536533567686304, 0.661960751670318, 0.725648429614889, 0.714209494237174, 0.669775240154245 + ], + [ + 1.0, 1.00771345051939, 0.985560178408803, 0.858512578205778, 0.683930986888496, + 0.527935488691156, 0.3918057610893, 0.314382527869049, 0.323698288913921, 0.378657449280973 + ], + ], +] + +EXPECTED_POPULATION = [ + [ + [ + 1.0, 0.988190700613443, 0.945887396889935, 0.880715286592028, 0.820079961642219, + 0.805543451673327, 0.847390213512399, 0.911186771519698, 0.963516042293813, 0.989000863070602 + ], + [ + 1.0, 0.987176432412105, 0.943914032049425, 0.881735467636543, 0.832225619095951, + 0.834306106268667, 0.88498000234694, 0.939790218211618, 0.968073231146879, 0.963863006144422 + ], + [ + 0.0, 0.0129222120656755, 0.0576374687847919, 0.125978662365299, 0.190583422639702, + 0.208877197419858, 0.169950900827144, 0.107255529146891, 0.0566579789205892, 0.0340643799998853 + ], + [ + 0.0, 0.013737247070628, 0.0569963384589609, 0.116041025988795, 0.160385019981892, + 0.152157990214599, 0.0957484420305465, 0.0369958197341599, 0.00962888724366689, 0.0174901622123016 + ], + ], + [ + [ + 1.0, 0.987669259560173, 0.931618260807432, 0.81659123470023, 0.664457320205344, + 0.526169369009, 0.433465673647445, 0.397172071715213, 0.414765943365967, 0.456314836307081 + ], + [ + 0.0, 0.0134363754081634, 0.0713607410419241, 0.187952301372471, 0.343318559068717, + 0.482375643794349, 0.575313507496633, 0.616581579094136, 0.605102485593769, 0.570355694262822 + ], + [ + 1.0, 0.986814269304548, 0.932538248731494, 0.819977997187482, 0.652260357061717, + 0.480531804700496, 0.350635065956558, 0.288012360567328, 0.293823455478383, 0.33302551860158 + ], + [ + 0.0, 0.0137331854404214, 0.0646145921213013, 0.1703199835656, 0.33646082929512, + 0.506186014446804, 0.631909230819356, 0.692704761904455, 0.681287681740471, 0.635125720195589 + ], + ], +] + +# Compare output to expected result for 10 points +NUM_SAMPLES = 10 +SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] + + +def test_example_cnot_withguardlevels(mpi_exec): + """Test CNOT gate optimization with guard levels using Python interface.""" + + Ne = [2, 2] + Ng = [2, 2] + freq01 = [4.80595, 4.8601] + selfkerr = [0.2, 0.2] + Jkl = [0.005] + favg = sum(freq01)/len(freq01) + rotfreq = favg*np.ones(len(freq01)) + T = 10.0 + + unitary = np.identity(4) + unitary[2, 2] = 0.0 + unitary[3, 3] = 0.0 + unitary[2, 3] = 1.0 + unitary[3, 2] = 1.0 + + spline_order = 0 + spline_knot_spacing = 1.0 + gamma_variation = 1.0 + control_enforce_BC = True + + n_osc = 2 + n_levels = 4 + + quandary = Quandary( + Ne=Ne, + Ng=Ng, + freq01=freq01, + selfkerr=selfkerr, + Jkl=Jkl, + rotfreq=rotfreq, + T=T, + targetgate=unitary, + verbose=False, + rand_seed=1234, + spline_order=spline_order, + spline_knot_spacing=spline_knot_spacing, + gamma_variation=gamma_variation, + control_enforce_BC=control_enforce_BC, + ) + + t, pt, qt, infidelity, energy, population = quandary.optimize( + mpi_exec=mpi_exec, + maxcores=4 + ) + + assert_results_equal( + t=t, + pt=pt, + qt=qt, + infidelity=infidelity, + energy=energy, + population=population, + T=T, + n_osc=n_osc, + n_levels=n_levels, + expected_length=EXPECTED_LENGTH, + expected_infidelity=EXPECTED_INFIDELITY, + expected_pt=EXPECTED_PT, + expected_qt=EXPECTED_QT, + expected_energy=EXPECTED_ENERGY, + expected_population=EXPECTED_POPULATION, + sample_indices=SAMPLE_INDICES + ) From 51d68b226eea19b0d4250d4c6221a1a5b12d5101 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 09:17:27 -0600 Subject: [PATCH 12/31] Add assert result sizes --- tests/python/utils.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/python/utils.py b/tests/python/utils.py index c2163d5b..64a77934 100644 --- a/tests/python/utils.py +++ b/tests/python/utils.py @@ -16,12 +16,20 @@ def assert_results_equal( assert len(t) == expected_length assert infidelity == approx(expected_infidelity, rel=REL_TOL, abs=ABS_TOL) + assert len(pt) == n_osc + assert len(qt) == n_osc + for i in range(n_osc): pt_samples = [pt[i][idx] for idx in sample_indices] qt_samples = [qt[i][idx] for idx in sample_indices] np.testing.assert_allclose(pt_samples, expected_pt[i], rtol=REL_TOL, atol=ABS_TOL) np.testing.assert_allclose(qt_samples, expected_qt[i], rtol=REL_TOL, atol=ABS_TOL) + assert len(energy) == n_osc + assert len(population) == n_osc + assert len(energy[0]) == n_levels + assert len(population[0]) == n_levels + for i in range(n_osc): for j in range(n_levels): energy_data = energy[i][j] From a67b8dde802a02c3009d4e2f34025b23da0a0f95 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 09:19:46 -0600 Subject: [PATCH 13/31] Remove unused constants --- tests/python/test_example_cnot.py | 3 --- tests/python/test_example_cnot_withguardlevels.py | 3 --- 2 files changed, 6 deletions(-) diff --git a/tests/python/test_example_cnot.py b/tests/python/test_example_cnot.py index 8931ef28..2b582d36 100644 --- a/tests/python/test_example_cnot.py +++ b/tests/python/test_example_cnot.py @@ -6,9 +6,6 @@ # Mark all tests in this file as regression tests pytestmark = pytest.mark.regression -REL_TOL = 1.0e-3 -ABS_TOL = 1.0e-10 - EXPECTED_LENGTH = 1222 EXPECTED_INFIDELITY = 9.579442993001841e-05 diff --git a/tests/python/test_example_cnot_withguardlevels.py b/tests/python/test_example_cnot_withguardlevels.py index b66be6b2..975959de 100644 --- a/tests/python/test_example_cnot_withguardlevels.py +++ b/tests/python/test_example_cnot_withguardlevels.py @@ -6,9 +6,6 @@ # Mark all tests in this file as regression tests pytestmark = pytest.mark.regression -REL_TOL = 1.0e-3 -ABS_TOL = 1.0e-10 - EXPECTED_LENGTH = 1806 EXPECTED_INFIDELITY = 0.480188055481932 From e962b9355a4e32054a51066eba25a7666abe05b4 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 09:27:52 -0600 Subject: [PATCH 14/31] Add piecewise constant control test --- ...est_example_piecewise_constant_controls.py | 176 ++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 tests/python/test_example_piecewise_constant_controls.py diff --git a/tests/python/test_example_piecewise_constant_controls.py b/tests/python/test_example_piecewise_constant_controls.py new file mode 100644 index 00000000..079b7653 --- /dev/null +++ b/tests/python/test_example_piecewise_constant_controls.py @@ -0,0 +1,176 @@ +import pytest +import numpy as np +from quandary import Quandary +from utils import assert_results_equal + +# Mark all tests in this file as regression tests +pytestmark = pytest.mark.regression + +EXPECTED_LENGTH = 1222 +EXPECTED_INFIDELITY = 9.257378541704941e-05 + +EXPECTED_PT = [ + [ + 1.39139720094627, -7.252599652352569, 4.47706380946171, 1.4726647949197, -1.19913109722706, + -0.33927983009720397, 2.3595839470663598, 0.677966851416258, -7.29939358795371, 1.8307265770282999 + ], + [ + -0.0733648695919313, -1.8066964117515698, 6.68654354551804, -7.48223143066535, 5.13340197845506, + 0.315835131695272, -3.73285646771321, 3.9510316874371703, -1.33023216001612, 0.124959893691128 + ], +] + +EXPECTED_QT = [ + [ + -0.196580437889947, -4.8278406547759305, 3.6663142384181198, -1.80095432726682, 2.0377228566350696, + -1.97182393857655, 1.80555521627317, -3.32011281124973, 5.250899308856639, -0.229292501560434 + ], + [ + -0.600034770502575, 0.688006184634379, 1.84509450260025, -0.24432814453945298, -2.6551546671386603, + 7.474760031265401, -5.53602372411908, 1.5594733148807, -0.347284269350974, 0.043548086484513006 + ], +] + +EXPECTED_ENERGY = [ + [ + [ + 0.0, 0.0699185568731366, 0.182704095177448, 0.249731600013859, 0.470426559112901, + 0.447394659045756, 0.224091290602656, 0.0779512718954046, 0.0650377137298774, 6.22310457717102e-05 + ], + [ + 0.0, 0.0930391138135943, 0.23743892043364, 0.413114574406591, 0.550666725329946, + 0.459127111006311, 0.334506254691026, 0.144006964319411, 0.0876700328696583, 5.11545031070254e-05 + ], + [ + 1.0, 0.905603791137994, 0.78486272542267, 0.726997485982308, 0.579209752236972, + 0.537878098046528, 0.671699851041516, 0.898618730017578, 0.933934688944795, 0.999917511582725 + ], + [ + 1.0, 0.931438538247787, 0.794994259093699, 0.610156339798125, 0.399696963538377, + 0.555600132161797, 0.769702603924927, 0.879423033910962, 0.913357564385493, 0.999969102986179 + ], + ], + [ + [ + 0.0, 0.0139344633082164, 0.171765683822903, 0.549172717868476, 0.50363006362385, + 0.311552152123449, 0.48754998982979, 0.249155258712721, 0.0145811031242011, 4.78606077574857e-05 + ], + [ + 1.0, 0.966726525551247, 0.819807830385754, 0.480870264221943, 0.509512169840825, + 0.673725087146305, 0.525882005326489, 0.740409351672592, 0.964210651043143, 0.999983363807341 + ], + [ + 0.0, 0.0539215132513357, 0.143866436910906, 0.382895489666004, 0.614576581108313, + 0.596135139899607, 0.633239241495552, 0.91162937655105, 0.984631303988097, 0.999934242114831 + ], + [ + 1.0, 0.965417497890465, 0.864560048886911, 0.587061528101101, 0.372281185307642, + 0.418587620841731, 0.353328763450195, 0.0988060135274491, 0.03657694219604, 3.4533577363965e-05 + ], + ], +] + +EXPECTED_POPULATION = [ + [ + [ + 1.0, 0.930081443141761, 0.817295904842303, 0.75026840001086, 0.529573440922835, + 0.552605340998218, 0.775908709447833, 0.922048728161235, 0.934962286333544, 0.999937769021251 + ], + [ + 1.0, 0.90696088618882, 0.762561079569985, 0.586885425621664, 0.449333274709712, + 0.540872889042223, 0.665493745364733, 0.855993035741032, 0.912329967191519, 0.999948845559214 + ], + [ + 0.0, 0.0943962088645299, 0.215137274581816, 0.273002514036944, 0.420790247791917, + 0.462121901990549, 0.328300149001861, 0.10138127003291, 0.0660653111133352, 8.24884791431685e-05 + ], + [ + 0.0, 0.0685614617674683, 0.205005740928792, 0.389843660228178, 0.600303036498988, + 0.444399867883395, 0.230297396125311, 0.120576966142488, 0.0866424356698608, 3.08970701760464e-05 + ], + ], + [ + [ + 1.0, 0.986065536706681, 0.828234316196848, 0.450827282156242, 0.496369936411885, + 0.688447847920526, 0.512450010220699, 0.750844741343919, 0.98541889693922, 0.999952139459265 + ], + [ + 0.0, 0.0332734744511672, 0.180192169617871, 0.519129735806312, 0.490487830198833, + 0.326274912902229, 0.47411799472927, 0.259590648387851, 0.0357893490180342, 1.66362549805352e-05 + ], + [ + 1.0, 0.946078486751189, 0.85613356309358, 0.617104510353248, 0.385423418920577, + 0.403864860137471, 0.366760758547825, 0.0883706234994385, 0.0153686960700334, 6.57579470371429e-05 + ], + [ + 0.0, 0.0345825021247907, 0.13543995113558, 0.412938471925202, 0.627718814729723, + 0.581412379203461, 0.646671236600043, 0.901193986526, 0.963423057859314, 0.999965466478991 + ], + ], +] + +# Compare output to expected result for 10 points +NUM_SAMPLES = 10 +SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] + + +def test_example_piecewise_constant_controls(mpi_exec): + """Test CNOT gate optimization with piecewise constant controls using Python interface.""" + + freq01 = [4.80595, 4.8601] + Jkl = [0.005] + favg = sum(freq01)/len(freq01) + rotfreq = favg*np.ones(len(freq01)) + T = 200.0 + + unitary = np.identity(4) + unitary[2, 2] = 0.0 + unitary[3, 3] = 0.0 + unitary[2, 3] = 1.0 + unitary[3, 2] = 1.0 + + spline_order = 0 + nsplines = 1000 + gamma_variation = 1.0 + control_enforce_BC = True + + n_osc = 2 + n_levels = 4 + + quandary = Quandary( + freq01=freq01, + Jkl=Jkl, + rotfreq=rotfreq, + T=T, + targetgate=unitary, + verbose=False, + rand_seed=1234, + spline_order=spline_order, + nsplines=nsplines, + gamma_variation=gamma_variation, + control_enforce_BC=control_enforce_BC + ) + + t, pt, qt, infidelity, energy, population = quandary.optimize( + mpi_exec=mpi_exec, + maxcores=1 + ) + + assert_results_equal( + t=t, + pt=pt, + qt=qt, + infidelity=infidelity, + energy=energy, + population=population, + T=T, + n_osc=n_osc, + n_levels=n_levels, + expected_length=EXPECTED_LENGTH, + expected_infidelity=EXPECTED_INFIDELITY, + expected_pt=EXPECTED_PT, + expected_qt=EXPECTED_QT, + expected_energy=EXPECTED_ENERGY, + expected_population=EXPECTED_POPULATION, + sample_indices=SAMPLE_INDICES + ) From 125622f3e6752805f458dd3dcb2cf10fe60218bf Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 10:04:34 -0600 Subject: [PATCH 15/31] Add qft test --- tests/python/test_example_qft.py | 356 +++++++++++++++++++++++++++++++ 1 file changed, 356 insertions(+) create mode 100644 tests/python/test_example_qft.py diff --git a/tests/python/test_example_qft.py b/tests/python/test_example_qft.py new file mode 100644 index 00000000..c92a79f7 --- /dev/null +++ b/tests/python/test_example_qft.py @@ -0,0 +1,356 @@ +import pytest +import numpy as np +from quandary import Quandary +from utils import assert_results_equal + +# Mark all tests in this file as regression tests +pytestmark = pytest.mark.regression + +EXPECTED_LENGTH = 3962 +EXPECTED_INFIDELITY = 0.8100341641826 + +EXPECTED_PT = [ + [ + 1.89222063059343, 0.117633784279222, 4.64028300872995, -0.0330222256817514, 0.126474004266131, + 2.30707289881048, -1.4716574716800102, -0.64754937225632, 2.0870897477143897, -3.5993747820438102 + ], + [ + 1.52517917625596, -2.0819686425195902, -6.16449162597314, -0.281926172015322, -2.29082646991941, + -1.35446004376719, -1.73027004354096, -1.6977449891882, -0.957325552016711, -0.66032285194334 + ], + [ + 3.38869388629721, 2.46448942091541, -5.18990161779653, 3.23625763732884, 0.21172074728456902, + -0.5306089302627199, 0.41489476880584397, -1.3511518437146701, 4.63365073411168, -3.2543080614975404 + ], +] + +EXPECTED_QT = [ + [ + 4.282471243949811, -1.90435084775161, 2.4105421852599003, 0.943664506103648, -1.48246991300687, + 1.32987551448728, -0.892631152337576, -2.95742646747671, -0.0736760483790147, -0.506870586543159 + ], + [ + 0.58274688212878, -0.5695945929016201, 0.777803006125851, -0.49131203698567594, -1.17510621256011, + 0.189918970015857, 1.79672837073849, 2.5547196576333, 1.96618771070137, 0.334327992279328 + ], + [ + 5.5996176445134695, -0.573714754569928, 3.04724887495592, 1.71942470756566, -0.877046652546983, + 1.3325727744462101, -3.90923703013145, 2.14761212417968, -0.288156432691881, -1.83963133335511 + ], +] + +EXPECTED_ENERGY = [ + [ + [ + 0.0, 0.0148607579341863, 0.116727716291541, 0.345569094665688, 0.420492333922264, + 0.502613489759601, 0.762594113234293, 0.785381772060781, 0.839275883284673, 0.929013124070048 + ], + [ + 0.0, 0.0146067840666606, 0.11754798689117, 0.346519320469488, 0.4183177982045, + 0.50464640883737, 0.762920218593243, 0.784725458623699, 0.84231160354798, 0.928128544837562 + ], + [ + 0.0, 0.0338745510923139, 0.148104595599986, 0.261474185761009, 0.43195551641555, + 0.542510481056473, 0.662377107041912, 0.773081817363954, 0.845641427818187, 0.836865832278204 + ], + [ + 0.0, 0.0341240967106616, 0.147546776276661, 0.258858419511221, 0.43301547251873, + 0.539199965870178, 0.659097143189277, 0.77199876021302, 0.842398924439871, 0.836306522946996 + ], + [ + 1.0, 0.965748235524499, 0.865577990181392, 0.656382457528953, 0.570472972368594, + 0.492800248880717, 0.25044370810636, 0.216403809620927, 0.161196371800905, 0.0904069492983516 + ], + [ + 1.0, 0.966339411555136, 0.864690415748644, 0.655340165309916, 0.572787651114863, + 0.491244274146324, 0.250766977888323, 0.217115378257751, 0.158045158422749, 0.0914413145926092 + ], + [ + 1.0, 0.985302944171115, 0.869398388325261, 0.736749605523319, 0.57695016774174, + 0.462035000315518, 0.324900968404589, 0.224944666181529, 0.153872876896255, 0.144029544672393 + ], + [ + 1.0, 0.98514321894768, 0.870406130707427, 0.739106751295271, 0.576008087756858, + 0.46495013117886, 0.326899763623054, 0.226348337749972, 0.157257753822829, 0.14380816732843 + ], + ], + [ + [ + 0.0, 0.0150176314942745, 0.0952237642656534, 0.231161654602016, 0.398633896748728, + 0.574531626678352, 0.606904878512938, 0.691463508739308, 0.71823660153979, 0.689891450405871 + ], + [ + 0.0, 0.0436936513275708, 0.0900374154017835, 0.274075384483282, 0.45593919442545, + 0.519637414444859, 0.675443884172963, 0.714424686347848, 0.751882030138024, 0.682769582193614 + ], + [ + 1.0, 0.945930049751258, 0.872572104725977, 0.772611799325123, 0.59459094265588, + 0.414239449304858, 0.412912804143054, 0.309664967276825, 0.279371139275707, 0.32776261067736 + ], + [ + 1.0, 0.957353693732795, 0.911385175111489, 0.728366698965441, 0.54374940635324, + 0.486344371457795, 0.339151646649223, 0.296283883722812, 0.25715846725817, 0.343462105862045 + ], + [ + 0.0, 0.0363821119988847, 0.0780954049938329, 0.280451133940982, 0.373900998934923, + 0.510075868735727, 0.643999882919365, 0.636071542458841, 0.647625442555183, 0.708855105449523 + ], + [ + 0.0, 0.0618240158469952, 0.0785064319332916, 0.329018008285977, 0.422648586637927, + 0.463065811003479, 0.717857009780474, 0.657369931484991, 0.692081777543554, 0.702806510604441 + ], + [ + 1.0, 0.963098590451382, 0.919650029375563, 0.717591526724565, 0.627551892694819, + 0.483502258280923, 0.342530133643125, 0.353977730515162, 0.342748871896153, 0.264516686473132 + ], + [ + 1.0, 0.976700255412085, 0.954529674217841, 0.666723793644168, 0.582985081535942, + 0.548603200112124, 0.261199760145823, 0.340743749394855, 0.310895669814159, 0.279935948363 + ], + ], + [ + [ + 0.0, 0.0655588575717349, 0.0953666127615174, 0.228383925020124, 0.348498151711691, + 0.273428303968501, 0.177039919128772, 0.0854203065177286, 0.0111626845277558, 0.0868589774774573 + ], + [ + 1.0, 0.914929104046163, 0.88771048417256, 0.772436424598468, 0.646985777288844, + 0.717671315173012, 0.828004337117799, 0.908248609385593, 0.984278193414155, 0.913728012465152 + ], + [ + 0.0, 0.0705031148232542, 0.13164941611602, 0.19830438969151, 0.311200364229386, + 0.356795065695722, 0.132688623811622, 0.107821000329134, 0.0208808765191582, 0.134883061129968 + ], + [ + 1.0, 0.948810126439896, 0.88510263569838, 0.801095701928753, 0.693256304101845, + 0.652049554109235, 0.862659139829962, 0.898626752171462, 0.983648171135189, 0.864925552983935 + ], + [ + 0.0, 0.0641308638686701, 0.0974321828809197, 0.229940677967645, 0.346693827090613, + 0.275634938859907, 0.179608035901328, 0.0850605815749401, 0.0114112298956469, 0.0880961143337944 + ], + [ + 1.0, 0.91662946844843, 0.885670860009623, 0.770859829069763, 0.649054297157375, + 0.716214484534058, 0.826309111174828, 0.908928466794254, 0.984230209711413, 0.91312708089424 + ], + [ + 0.0, 0.0716084146957608, 0.129827595084711, 0.1957134832151, 0.31075703788224, + 0.35341587787003, 0.128296077572829, 0.106742758530496, 0.0194842289805046, 0.135561378058802 + ], + [ + 1.0, 0.947830050103069, 0.887240213265323, 0.803265568539764, 0.69355424058736, + 0.654790459802924, 0.86539475548002, 0.899151524732232, 0.98490440578269, 0.862819822619949 + ], + ], +] + +EXPECTED_POPULATION = [ + [ + [ + 1.0, 0.985139242076125, 0.883272283718809, 0.654430905344746, 0.579507666088186, + 0.497386510250861, 0.237405886776199, 0.214618227949722, 0.160724116725837, 0.0709868759404779 + ], + [ + 1.0, 0.98539321593334, 0.88245201310884, 0.65348067953056, 0.581682201795565, + 0.495353591162707, 0.237079781406884, 0.215274541376461, 0.157688396452205, 0.0718714551627529 + ], + [ + 1.0, 0.96612544890773, 0.851895404400098, 0.738525814239158, 0.568044483584635, + 0.457489518943723, 0.337622892958313, 0.226918182636287, 0.15435857218206, 0.163134167722058 + ], + [ + 1.0, 0.965875903289341, 0.852453223723351, 0.741141580488827, 0.566984527481334, + 0.460800034129898, 0.340902856810848, 0.228001239787137, 0.157601075560305, 0.163693477053312 + ], + [ + 0.0, 0.034251764475504, 0.134422009818618, 0.343617542471092, 0.429527027631466, + 0.507199751119354, 0.749556291893752, 0.783596190379213, 0.838803628199257, 0.90959305070191 + ], + [ + 0.0, 0.0336605884449191, 0.135309584251455, 0.344659834690291, 0.427212348885368, + 0.50875572585392, 0.749233022111954, 0.782884621742538, 0.841954841577546, 0.908558685407704 + ], + [ + 0.0, 0.0146970558288883, 0.13060161167475, 0.263250394476727, 0.423049832258321, + 0.537964999684555, 0.675099031595522, 0.775055333818613, 0.846127123103906, 0.855970455327864 + ], + [ + 0.0, 0.014856781052379, 0.129593869292673, 0.260893248704942, 0.423991912243378, + 0.535049868821387, 0.673100236377226, 0.773651662250322, 0.842742246177471, 0.856191832671892 + ], + ], + [ + [ + 1.0, 0.984982368516037, 0.904776235744696, 0.768838345408418, 0.601366103261722, + 0.425468373332111, 0.393095121497554, 0.308536491271195, 0.28176339847072, 0.310108549604656 + ], + [ + 1.0, 0.956306348672429, 0.909962584598226, 0.725924615516766, 0.544060805574615, + 0.480362585555218, 0.324556115827164, 0.285575313652313, 0.248117969862161, 0.317230417806702 + ], + [ + 0.0, 0.0540699502487857, 0.127427895274107, 0.227388200675044, 0.405409057344305, + 0.585760550695338, 0.587087195857171, 0.690335032723416, 0.72062886072454, 0.672237389322902 + ], + [ + 0.0, 0.0426463062672075, 0.0886148248885225, 0.271633301034608, 0.456250593646824, + 0.513655628542281, 0.660848353350902, 0.703716116277345, 0.742841532742006, 0.656537894138263 + ], + [ + 1.0, 0.963617888001119, 0.921904595006177, 0.719548866059064, 0.626099001065137, + 0.489924131264345, 0.356000117080747, 0.363928457541299, 0.352374557444978, 0.291144894550738 + ], + [ + 1.0, 0.93817598415306, 0.921493568066807, 0.670981991714231, 0.577351413362303, + 0.536934188996764, 0.282142990219803, 0.342630068515299, 0.307918222456741, 0.297193489395872 + ], + [ + 0.0, 0.0369014095486213, 0.0803499706244474, 0.282408473275481, 0.372448107305242, + 0.51649774171915, 0.657469866356986, 0.64602226948498, 0.657251128104008, 0.735483313527125 + ], + [ + 0.0, 0.0232997445879744, 0.0454703257822588, 0.333276206356045, 0.417014918464293, + 0.451396799888122, 0.738800239854458, 0.659256250605438, 0.689104330186141, 0.720064051637323 + ], + ], + [ + [ + 1.0, 0.934441142438577, 0.904633387248832, 0.771616074990311, 0.65150184829876, + 0.726571696041962, 0.822960080881721, 0.914579693492775, 0.988837315482754, 0.913141022533069 + ], + [ + 0.0, 0.0850708959538377, 0.11228951582745, 0.22756357540158, 0.353014222711221, + 0.282328684827064, 0.171995662882329, 0.091751390614568, 0.0157218065860296, 0.0862719875351632 + ], + [ + 1.0, 0.929496885176789, 0.868350583884063, 0.801695610308657, 0.688799635770799, + 0.643204934304475, 0.867311376188603, 0.892178999671106, 0.979119123481089, 0.865116938870295 + ], + [ + 0.0, 0.0511898735601057, 0.114897364301632, 0.198904298071296, 0.306743695898219, + 0.347950445890841, 0.137340860170163, 0.101373247828695, 0.0163518288649865, 0.135074447016374 + ], + [ + 1.0, 0.935869136131333, 0.902567817119091, 0.770059322032401, 0.653306172909447, + 0.724365061140165, 0.820391964098783, 0.9149394184252, 0.988588770104514, 0.911903885666467 + ], + [ + 0.0, 0.0833705315516252, 0.114329139990476, 0.229140170930445, 0.350945702842855, + 0.283785515466185, 0.173690888825448, 0.0910715332060352, 0.015769790288882, 0.0868729191060732 + ], + [ + 1.0, 0.928391585304243, 0.870172404915299, 0.804286516784946, 0.689242962117821, + 0.646584122130044, 0.871703922427283, 0.893257241469646, 0.980515771019656, 0.864438621941455 + ], + [ + 0.0, 0.0521699498969902, 0.112759786734776, 0.196734431460449, 0.306445759412875, + 0.345209540197323, 0.13460524452026, 0.100848475268061, 0.0150955942176098, 0.137180177380373 + ], + ], +] + +# Compare output to expected result for 10 points +NUM_SAMPLES = 10 +SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] + + +def get_QFT_gate(dim): + """Generate QFT gate.""" + gate_Hd = np.zeros((dim, dim), dtype=complex) + om_d = np.exp(1j*2*np.pi/dim) + + for j in range(dim): + for k in range(dim): + gate_Hd[j, k] = om_d**(j*k) / np.sqrt(dim) + + return gate_Hd + + +def test_example_qft(mpi_exec): + """Test 3-qubit QFT gate optimization using Python interface.""" + + # Parameters for 3-qubit case + nqubits = 3 + freq01_all = [5.18, 5.12, 5.06, 5.0, 4.94] + Jkl_coupling = 5e-3 + + # Set up frequency vector + freq01 = [] + Ne = [] + Ng = [] + for i in range(nqubits): + freq01.append(freq01_all[len(freq01_all)-i-1]) + Ne.append(2) + Ng.append(0) + + # Set up qubit CHAIN coupling + Jkl = [] + for i in range(len(Ne)): + for j in range(i+1, len(Ne)): + if j == i+1: + val = Jkl_coupling + else: + val = 0.0 + Jkl.append(val) + + T = 100 + favg = sum(freq01)/len(freq01) + rotfreq = favg*np.ones(len(freq01)) + unitary = get_QFT_gate(np.prod(Ne)) + + Pmin = 150*2 + spline_knot_spacing = 3.0 + maxctrl_MHz = 25.0 + cw_amp_thres = 5e-2 + cw_prox_thres = 1e-3 + gamma_energy = 1e-4 + gamma_tik0 = 1e-3 + gamma_dpdm = 0.0 + + n_osc = 3 + n_levels = 8 + + quandary = Quandary( + Ne=Ne, + Ng=Ng, + freq01=freq01, + Jkl=Jkl, + rotfreq=rotfreq, + T=T, + Pmin=Pmin, + spline_knot_spacing=spline_knot_spacing, + targetgate=unitary, + verbose=False, + rand_seed=1234, + maxctrl_MHz=maxctrl_MHz, + cw_amp_thres=cw_amp_thres, + cw_prox_thres=cw_prox_thres, + gamma_energy=gamma_energy, + gamma_tik0=gamma_tik0, + gamma_dpdm=gamma_dpdm, + maxiter=10, + ) + + t, pt, qt, infidelity, energy, population = quandary.optimize( + mpi_exec=mpi_exec, + maxcores=4 + ) + + assert_results_equal( + t=t, + pt=pt, + qt=qt, + infidelity=infidelity, + energy=energy, + population=population, + T=T, + n_osc=n_osc, + n_levels=n_levels, + expected_length=EXPECTED_LENGTH, + expected_infidelity=EXPECTED_INFIDELITY, + expected_pt=EXPECTED_PT, + expected_qt=EXPECTED_QT, + expected_energy=EXPECTED_ENERGY, + expected_population=EXPECTED_POPULATION, + sample_indices=SAMPLE_INDICES + ) From 39f46ad57a8e916f976bf1a194ce82145329dbb6 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 10:08:21 -0600 Subject: [PATCH 16/31] Add state to state test --- tests/python/test_example_state_to_state.py | 99 +++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/python/test_example_state_to_state.py diff --git a/tests/python/test_example_state_to_state.py b/tests/python/test_example_state_to_state.py new file mode 100644 index 00000000..309b2dfc --- /dev/null +++ b/tests/python/test_example_state_to_state.py @@ -0,0 +1,99 @@ +import pytest +import numpy as np +from quandary import Quandary +from utils import assert_results_equal + +# Mark all tests in this file as regression tests +pytestmark = pytest.mark.regression + +EXPECTED_LENGTH = 1652 +EXPECTED_INFIDELITY = 9.002940985003427e-05 + +EXPECTED_PT = [ + [ + -0.430435057899144, 0.518899252233085, 0.0553961310496238, -2.73778883011829, 0.836879517793378, + 1.68236101443972, 1.2272994947870701, -1.13889765832405, 2.7441415074662903, 1.68020127607366 + ], +] + +EXPECTED_QT = [ + [ + -1.21262883183835, -2.77343358982429, -2.7674896542845198, -2.81712074264336, -2.71698129305364, + -2.7908352239901797, -2.65458260105392, -2.57461802357684, -2.0247101898358, -1.26152275887394 + ], +] + +EXPECTED_ENERGY = [ + [ + [ + 0.0, 0.00694252103065461, 0.0331419031652123, 0.0744089523543541, 0.137405174039168, + 0.205169499219536, 0.282367422349298, 0.368813810556003, 0.455572996577133, 0.496383344017663 + ], + ], +] + +EXPECTED_POPULATION = [ + [ + [ + 1.0, 0.99305963258305, 0.966873105642978, 0.925633731947042, 0.862637301677907, + 0.794942546965401, 0.717715083633589, 0.631227300284134, 0.54475917511335, 0.503663559455785 + ], + ], +] + +# Compare output to expected result for 10 points +NUM_SAMPLES = 10 +SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] + + +def test_example_state_to_state(mpi_exec): + """Test state-to-state preparation using Python interface.""" + + Ne = [2] + Ng = [1] + freq01 = [4.10595] + selfkerr = [0.2198] + T = 50.0 + maxctrl_MHz = 4.0 + initialcondition = [1.0, 0.0] + targetstate = [1.0/np.sqrt(2), 1.0/np.sqrt(2)] + n_osc = 1 + n_levels = 1 + + quandary = Quandary( + Ne=Ne, + Ng=Ng, + freq01=freq01, + selfkerr=selfkerr, + maxctrl_MHz=maxctrl_MHz, + initialcondition=initialcondition, + targetstate=targetstate, + T=T, + tol_infidelity=1e-5, + rand_seed=4321, + verbose=False + ) + + t, pt, qt, infidelity, energy, population = quandary.optimize( + mpi_exec=mpi_exec, + maxcores=1 + ) + + assert_results_equal( + t=t, + pt=pt, + qt=qt, + infidelity=infidelity, + energy=energy, + population=population, + T=T, + n_osc=n_osc, + n_levels=n_levels, + expected_length=EXPECTED_LENGTH, + expected_infidelity=EXPECTED_INFIDELITY, + expected_pt=EXPECTED_PT, + expected_qt=EXPECTED_QT, + expected_energy=EXPECTED_ENERGY, + expected_population=EXPECTED_POPULATION, + sample_indices=SAMPLE_INDICES + ) From 45167de1ef895a3c5b456d5dc33cd602f2a70e6f Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 10:13:18 -0600 Subject: [PATCH 17/31] Add swap02 test --- tests/python/test_example_swap02.py | 111 ++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 tests/python/test_example_swap02.py diff --git a/tests/python/test_example_swap02.py b/tests/python/test_example_swap02.py new file mode 100644 index 00000000..3c341716 --- /dev/null +++ b/tests/python/test_example_swap02.py @@ -0,0 +1,111 @@ +import pytest +from quandary import Quandary +from utils import assert_results_equal + +# Mark all tests in this file as regression tests +pytestmark = pytest.mark.regression + +EXPECTED_LENGTH = 9906 +EXPECTED_INFIDELITY = 4.30051844739765e-05 + +EXPECTED_PT = [ + [ + 1.5223974313145099, 0.192367631943629, 5.2322350273611695, 0.270716585386651, 4.38454614717518, + 1.64522208663637, 4.27356481827349, 3.56918803082488, 0.809746914647563, 2.49847938610855 + ], +] + +EXPECTED_QT = [ + [ + 1.1486887461253, 3.65174025044863, 3.38068715198705, 1.69637577485486, 3.89987855203689, + 1.02123892969852, 4.5603555728564, -0.6527678876090169, 4.1927845343931, 0.519653744346407 + ], +] + +EXPECTED_ENERGY = [ + [ + [ + 0.0, 0.0234550545874589, 0.151322202170957, 0.38091326425715, 0.719219922127726, + 1.07914866155792, 1.38417447451341, 1.73449380418287, 1.90878994427285, 2.00000694611498 + ], + [ + 1.0, 1.03449485473264, 1.15972091768289, 1.18383141548819, 1.21165277321355, + 1.20900227532045, 1.21269089383777, 1.11510843776812, 1.0552883588209, 1.00005015890853 + ], + [ + 2.0, 1.94250327364116, 1.68998722508855, 1.43543971159315, 1.070068198256, + 0.711977722635159, 0.404183040483815, 0.151128785805321, 0.0373575963912251, 8.02625600351541e-05 + ], + ], +] + +EXPECTED_POPULATION = [ + [ + [ + 1.0, 0.97686084850513, 0.859760974702525, 0.661516606458905, 0.413421449887264, + 0.205924265997096, 0.0705988862580945, 0.01147031596463, 0.00057333624469326, 3.75770574331089e-06 + ], + [ + 0.0, 0.0226342148878521, 0.127263830043423, 0.268727310512903, 0.379204026955525, + 0.377295442001178, 0.277750731738844, 0.13081630109352, 0.0355226617637922, 1.27899738623382e-05 + ], + [ + 0.0, 0.000504783498382912, 0.0129728034877987, 0.0697471926129869, 0.207353891744709, + 0.41674941229836, 0.651616413277044, 0.857685579183397, 0.96388427223235, 0.9999635620352 + ], + ], +] + +# Compare output to expected result for 10 points +NUM_SAMPLES = 10 +SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] + + +def test_example_swap02(mpi_exec): + """Test SWAP 0-2 gate optimization using Python interface.""" + + Ne = [3] + Ng = [1] + freq01 = [4.10595] + selfkerr = [0.2198] + T = 100.0 + maxctrl_MHz = 8.0 + unitary = [[0, 0, 1], [0, 1, 0], [1, 0, 0]] + n_osc = 1 + n_levels = 3 + + quandary = Quandary( + Ne=Ne, + Ng=Ng, + freq01=freq01, + selfkerr=selfkerr, + maxctrl_MHz=maxctrl_MHz, + targetgate=unitary, + T=T, + rand_seed=1234, + verbose=False + ) + + t, pt, qt, infidelity, energy, population = quandary.optimize( + mpi_exec=mpi_exec, + maxcores=4 + ) + + assert_results_equal( + t=t, + pt=pt, + qt=qt, + infidelity=infidelity, + energy=energy, + population=population, + T=T, + n_osc=n_osc, + n_levels=n_levels, + expected_length=EXPECTED_LENGTH, + expected_infidelity=EXPECTED_INFIDELITY, + expected_pt=EXPECTED_PT, + expected_qt=EXPECTED_QT, + expected_energy=EXPECTED_ENERGY, + expected_population=EXPECTED_POPULATION, + sample_indices=SAMPLE_INDICES + ) From f65005420a0f33238afcb96ce499f6e8c946b8c4 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 10:18:35 -0600 Subject: [PATCH 18/31] Add swap12 test --- tests/python/test_example_swap12.py | 166 ++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 tests/python/test_example_swap12.py diff --git a/tests/python/test_example_swap12.py b/tests/python/test_example_swap12.py new file mode 100644 index 00000000..4d86c45c --- /dev/null +++ b/tests/python/test_example_swap12.py @@ -0,0 +1,166 @@ +import pytest +import numpy as np +from quandary import Quandary +from utils import assert_results_equal + +# Mark all tests in this file as regression tests +pytestmark = pytest.mark.regression + +EXPECTED_LENGTH = 2770 +EXPECTED_INFIDELITY = 7.825770499303442e-05 + +EXPECTED_PT = [ + [ + 1.8184215029697302, 6.53541488985085, -12.8815729540948, 13.377779957533601, 15.2559573747476, + -8.55501991773305, -11.571287232539401, 14.5042812158254, -15.2528144059597, -3.12330522923919 + ], + [ + -2.87395494164515, 16.170015973460497, 11.9340520593027, 12.4971142034791, -13.5854201479712, + 7.59926941274165, -4.00248319501313, -12.8298360004956, -6.616155043728059, 1.7176979043401601 + ], +] + +EXPECTED_QT = [ + [ + 3.01935945129119, 16.9528419819601, -4.97870306003347, -6.96804772378882, -0.690015284496415, + 18.0510636076456, -15.4461172849659, 2.94473029913097, -4.26291478812709, 5.15980230386097 + ], + [ + -6.72607543122026, 8.02402525815179, -14.674541268055801, -12.9299891301557, 12.4372973005706, + 10.031606552642499, -17.2180727670311, -10.4295835579693, -4.794657547857811, 6.95039468450853 + ], +] + +EXPECTED_ENERGY = [ + [ + [ + 0.0, 0.47592735741237, 0.920916877121964, 0.806725800976249, 0.655347266057245, + 0.40956010822443, 0.523495640405955, 0.389958416870123, 0.894309878756772, 2.78351042400667e-05 + ], + [ + 0.0, 0.506436069839158, 0.894041487982348, 0.482825459874139, 0.263276647075983, + 0.706421192747589, 0.326892966495981, 0.546600128673876, 0.13730502998382, 0.999928014745447 + ], + [ + 1.0, 0.517968156465863, 0.0990805037166172, 0.323988697368065, 0.517432001627619, + 0.162284449342756, 0.321486318542869, 0.492744199081024, 0.806922263339182, 7.61332704578777e-05 + ], + [ + 1.0, 0.499668416053135, 0.0859611313258037, 0.386460041957824, 0.563944085315957, + 0.721734249708354, 0.82812507503546, 0.570697256338126, 0.161462827349231, 0.999968016951936 + ], + ], + [ + [ + 0.0, 0.746465361502474, 0.446030321847237, 0.751926392244149, 0.438374046543845, + 0.74788628530837, 0.436255115167154, 0.770339060727562, 0.624611223052237, 2.6314964006988e-05 + ], + [ + 1.0, 0.248921057424824, 0.602804594052578, 0.156112931294798, 0.592388881415718, + 0.565265558947553, 0.492594960169373, 0.912502253042697, 0.677377691676097, 7.8641614022487e-05 + ], + [ + 0.0, 0.69109059043582, 0.612946285346014, 0.774327542815432, 0.102443880083346, + 0.188841270175233, 0.716258624483362, 0.282332897067584, 0.397788656312723, 0.999929501726309 + ], + [ + 1.0, 0.313522990987136, 0.338218798604214, 0.317633134502716, 0.866793191462009, + 0.498006885137639, 0.354891300964239, 0.0348257898399821, 0.300222429802531, 0.999965541769708 + ], + ], +] + +EXPECTED_POPULATION = [ + [ + [ + 1.0, 0.524072642589295, 0.0790831228826006, 0.193274199031723, 0.344652733956227, + 0.590439891793979, 0.476504359618558, 0.610041583159233, 0.105690121275478, 0.999972164930443 + ], + [ + 1.0, 0.493563930162257, 0.105958512020345, 0.517174540132416, 0.736723352935244, + 0.2935788072667, 0.67310703352522, 0.45339987135301, 0.862694970044658, 7.1985285512568e-05 + ], + [ + 0.0, 0.482031843535495, 0.900919496286575, 0.676011302636772, 0.48256799838042, + 0.837715550669844, 0.678513681476111, 0.507255800952662, 0.193077736696033, 0.999923866768033 + ], + [ + 0.0, 0.500331583949654, 0.914038868680122, 0.613539958052361, 0.436055914699638, + 0.278265750311728, 0.171874924988557, 0.42930274369637, 0.838537172687105, 3.198308739064e-05 + ], + ], + [ + [ + 1.0, 0.253534638499192, 0.553969678157327, 0.248073607763823, 0.561625953469628, + 0.25211371471004, 0.563744884857359, 0.229660939301793, 0.375388776980013, 0.999973685070676 + ], + [ + 0.0, 0.751078942576592, 0.397195405950115, 0.843887068711757, 0.407611118595508, + 0.434734441066736, 0.507405039851828, 0.0874977469841897, 0.322622308352381, 0.999921358416937 + ], + [ + 1.0, 0.308909409565538, 0.387053714657178, 0.225672457189406, 0.897556119924694, + 0.811158729837367, 0.283741375535618, 0.717667102966101, 0.602211343722492, 7.0498312181619e-05 + ], + [ + 0.0, 0.686477009015653, 0.661781201401711, 0.682366865507469, 0.133206808553586, + 0.501993114882443, 0.645108699059778, 0.965174210194513, 0.699777570233805, 3.44582696187683e-05 + ], + ], +] + +# Compare output to expected result for 10 points +NUM_SAMPLES = 10 +SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] + +def test_example_swap12(mpi_exec): + """Test SWAP 1-2 gate optimization using Python interface.""" + + freq01 = [5.12, 5.06] + Jkl = [5.0e-3] + favg = sum(freq01)/len(freq01) + rotfreq = favg*np.ones(len(freq01)) + T = 200.0 + maxctrl_MHz = 30.0*np.ones(len(freq01)) + unitary = np.identity(4) + unitary[1, 1] = 0.0 + unitary[1, 2] = 1.0 + unitary[2, 1] = 1.0 + unitary[2, 2] = 0.0 + n_osc = 2 + n_levels = 4 + + quandary = Quandary( + freq01=freq01, + Jkl=Jkl, + rotfreq=rotfreq, + T=T, + maxctrl_MHz=maxctrl_MHz, + targetgate=unitary, + verbose=False, + rand_seed=1234, + ) + + t, pt, qt, infidelity, energy, population = quandary.optimize( + mpi_exec=mpi_exec, + maxcores=4 + ) + + assert_results_equal( + t=t, + pt=pt, + qt=qt, + infidelity=infidelity, + energy=energy, + population=population, + T=T, + n_osc=n_osc, + n_levels=n_levels, + expected_length=EXPECTED_LENGTH, + expected_infidelity=EXPECTED_INFIDELITY, + expected_pt=EXPECTED_PT, + expected_qt=EXPECTED_QT, + expected_energy=EXPECTED_ENERGY, + expected_population=EXPECTED_POPULATION, + sample_indices=SAMPLE_INDICES + ) From 940716890121bac45aad9f74e783dd85a94b7cbf Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 11:30:28 -0600 Subject: [PATCH 19/31] Add note about population levels --- tests/python/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/utils.py b/tests/python/utils.py index 64a77934..d6c72b4a 100644 --- a/tests/python/utils.py +++ b/tests/python/utils.py @@ -36,6 +36,7 @@ def assert_results_equal( energy_samples = [energy_data[idx] for idx in sample_indices] np.testing.assert_allclose(energy_samples, expected_energy[i][j], rtol=REL_TOL, atol=ABS_TOL) + # Note: only comparing population for the first level (index 0) pop_data = population[i][j] pop_samples = [pop_data[0, idx] for idx in sample_indices] np.testing.assert_allclose(pop_samples, expected_population[i][j], rtol=REL_TOL, atol=ABS_TOL) From a6a158a4003bfd1a2d4061bd23713ac01a1699a3 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 11:30:42 -0600 Subject: [PATCH 20/31] Add spinchain tests --- tests/python/test_example_spinchain.py | 220 +++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 tests/python/test_example_spinchain.py diff --git a/tests/python/test_example_spinchain.py b/tests/python/test_example_spinchain.py new file mode 100644 index 00000000..0c6a5718 --- /dev/null +++ b/tests/python/test_example_spinchain.py @@ -0,0 +1,220 @@ +import pytest +import numpy as np +from quandary import Quandary +from utils import assert_results_equal + +# Mark all tests in this file as regression tests +pytestmark = pytest.mark.regression + +EXPECTED_LENGTH = 1001 +EXPECTED_INFIDELITY = 1.0 + +EXPECTED_PT = np.zeros((8, 10)) + +EXPECTED_QT = np.zeros((8, 10)) + +EXPECTED_ENERGY = [ + [ + [ + 1.0, 0.836078135681189, 0.677080386733117, 0.670628110768053, 0.92300799094264, + 0.837975552236461, 0.805823129197361, 0.853504682689832, 0.866549990398791, 0.806457068240172 + ], + ], + [ + [ + 1.0, 0.636388342297137, 0.487092110002376, 0.54413722703352, 0.771657513370998, + 0.666408072335883, 0.612024569159776, 0.824676325668549, 0.419674820100051, 0.702460959050306 + ], + ], + [ + [ + 1.0, 0.66748361234842, 0.46347452987769, 0.744184045857701, 0.802417472822974, + 0.770651675593258, 0.63234540776416, 0.383254206449323, 0.785911746247381, 0.708761215708732 + ], + ], + [ + [ + 1.0, 0.66009154910948, 0.492257221761028, 0.619014865091762, 0.513571520786539, + 0.653392374584252, 0.629386909048527, 0.660832003911264, 0.682292516326034, 0.707144820937568 + ], + ], + [ + [ + 0.0, 0.500108453041719, 0.534431835905109, 0.47752894868167, 0.242450496754744, + 0.293746749349346, 0.518698522063187, 0.637304278577435, 0.659394914028851, 0.310680732457375 + ], + ], + [ + [ + 0.0, 0.269069449733598, 0.243034570485257, 0.253350482543049, 0.30230026530336, + 0.14570551680724, 0.267274075325126, 0.112004940016193, 0.274423918272602, 0.237851814417754 + ], + ], + [ + [ + 0.0, 0.243117466829247, 0.555330150338723, 0.391204137042619, 0.197412587591839, + 0.179316513251891, 0.195590432087981, 0.371427834282674, 0.151967524907041, 0.118392848324015 + ], + ], + [ + [ + 0.0, 0.187662990975368, 0.547299194929018, 0.299952183030103, 0.247182152491542, + 0.452803545922466, 0.338856955450839, 0.156995728517847, 0.159784569848526, 0.408250541009659 + ], + ], +] + +EXPECTED_POPULATION = [ + [ + [ + 0.0, 0.16392186432285, 0.322919613274963, 0.329371889244066, 0.0769920090735191, + 0.162024447783738, 0.194176870826878, 0.146495317338447, 0.133450009633529, 0.193542931796223 + ], + ], + [ + [ + 0.0, 0.363611657706903, 0.512907890005703, 0.455862772978599, 0.228342486645161, + 0.333591927684317, 0.387975430864463, 0.17532367435973, 0.580325179932268, 0.297539040986089 + ], + ], + [ + [ + 0.0, 0.33251638765562, 0.536525470130389, 0.255815954154418, 0.197582527193185, + 0.229348324426941, 0.367654592260079, 0.616745793578956, 0.214088253784938, 0.291238784327663 + ], + ], + [ + [ + 0.0, 0.339908450894559, 0.507742778247051, 0.380985134920358, 0.48642847922962, + 0.346607625435947, 0.370613090975712, 0.339167996117016, 0.317707483706285, 0.292855179098828 + ], + ], + [ + [ + 1.0, 0.499891546962321, 0.465568164102971, 0.522471051330449, 0.757549503261415, + 0.706253250670854, 0.481301477961052, 0.362695721450844, 0.340605086003469, 0.689319267579021 + ], + ], + [ + [ + 1.0, 0.730930550270442, 0.756965429522822, 0.74664951746907, 0.697699734712799, + 0.854294483212959, 0.732725924699113, 0.887995060012086, 0.725576081759717, 0.762148185618641 + ], + ], + [ + [ + 1.0, 0.756882533174792, 0.444669849669357, 0.6087958629695, 0.80258741242432, + 0.820683486768308, 0.804409567936258, 0.628572165745605, 0.848032475125278, 0.88160715171238 + ], + ], + [ + [ + 1.0, 0.812337009028671, 0.452700805079061, 0.700047816982017, 0.752817847524617, + 0.547196454097733, 0.6611430445734, 0.843004271510433, 0.840215430183794, 0.591749459026737 + ], + ], +] + +# Compare output to expected result for 10 points +NUM_SAMPLES = 10 +SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] + + +def mapCoeffs_SpinChainToQuandary(N: int, h: list, U: list, J: list): + """ + Map spin chain coefficients J, U and h onto Quandary coefficients + """ + # 01 transition frequencies [GHz] per site (omega_q) + freq01 = np.zeros(N) + for i in range(1, N-1): + freq01[i] = (-2*h[i] -2*U[i] -2*U[i-1]) / (2*np.pi) + freq01[0] = (-2*h[0] - 2*U[0]) / (2*np.pi) + freq01[N-1] = (-2*h[N-1] - 2*U[N-2]) / (2*np.pi) + + # Jkl and Xi term [GHz] + Jkl = [] + crosskerr = [] + couplingID = 0 + for i in range(N): + for j in range(i+1, N): + if j == i+1: # linear chain coupling + valJ = - 2*J[couplingID] / (2*np.pi) + valC = - 4*U[couplingID] / (2*np.pi) + else: + valJ = 0.0 + valC = 0.0 + Jkl.append(valJ) + crosskerr.append(valC) + couplingID += 1 + return freq01, crosskerr, Jkl + + +def test_example_spinchain(mpi_exec): + """Test spin chain simulation using Python interface.""" + + N = 8 + U_amp = 1.0 + J_amp = 1.0 + np.random.seed(9001) # Set seed for reproducibility + h = np.random.uniform(-U_amp, U_amp, N) + U = np.zeros(N) + J = J_amp * np.ones(N) + + # Specify the initial state (domain wall |111...000>) + initstate = np.zeros(N, dtype=int) + for i in range(int(N/2)): + initstate[i] = 1 + + # Set the simulation duration and step size + T = 10.0 + dT = 0.01 + + # Prepare Quandary + initcondstr = "pure, " + for e in initstate: + initcondstr += str(e) + ", " + + freq01, crosskerr, Jkl = mapCoeffs_SpinChainToQuandary(N, h, U, J) + + n_osc = N + n_levels = 1 + + quandary = Quandary( + Ne=[2 for _ in range(N)], + Ng=[0 for _ in range(N)], + freq01=freq01, + rotfreq=np.zeros(N), + crosskerr=crosskerr, + Jkl=Jkl, + initialcondition=initcondstr, + T=T, + dT=dT, + initctrl_MHz=0.0, + carrier_frequency=[[0.0] for _ in range(N)], + verbose=False + ) + + # Run forward simulation + t, pt, qt, infidelity, energy, population = quandary.simulate( + mpi_exec=mpi_exec, + maxcores=1, + ) + + assert_results_equal( + t=t, + pt=pt, + qt=qt, + infidelity=infidelity, + energy=energy, + population=population, + T=T, + n_osc=n_osc, + n_levels=n_levels, + expected_length=EXPECTED_LENGTH, + expected_infidelity=EXPECTED_INFIDELITY, + expected_pt=EXPECTED_PT, + expected_qt=EXPECTED_QT, + expected_energy=EXPECTED_ENERGY, + expected_population=EXPECTED_POPULATION, + sample_indices=SAMPLE_INDICES + ) From 56d35cca4e7da4875bc4b3a9c4324780c08f6667 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 11:36:15 -0600 Subject: [PATCH 21/31] Whitespace cleanups --- tests/python/test_example_spinchain.py | 2 +- tests/python/test_example_swap12.py | 1 + tests/python/utils.py | 5 ++--- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/python/test_example_spinchain.py b/tests/python/test_example_spinchain.py index 0c6a5718..72245073 100644 --- a/tests/python/test_example_spinchain.py +++ b/tests/python/test_example_spinchain.py @@ -127,7 +127,7 @@ def mapCoeffs_SpinChainToQuandary(N: int, h: list, U: list, J: list): # 01 transition frequencies [GHz] per site (omega_q) freq01 = np.zeros(N) for i in range(1, N-1): - freq01[i] = (-2*h[i] -2*U[i] -2*U[i-1]) / (2*np.pi) + freq01[i] = (-2*h[i] - 2*U[i] - 2*U[i-1]) / (2*np.pi) freq01[0] = (-2*h[0] - 2*U[0]) / (2*np.pi) freq01[N-1] = (-2*h[N-1] - 2*U[N-2]) / (2*np.pi) diff --git a/tests/python/test_example_swap12.py b/tests/python/test_example_swap12.py index 4d86c45c..1c11cd18 100644 --- a/tests/python/test_example_swap12.py +++ b/tests/python/test_example_swap12.py @@ -113,6 +113,7 @@ NUM_SAMPLES = 10 SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] + def test_example_swap12(mpi_exec): """Test SWAP 1-2 gate optimization using Python interface.""" diff --git a/tests/python/utils.py b/tests/python/utils.py index d6c72b4a..1fd53832 100644 --- a/tests/python/utils.py +++ b/tests/python/utils.py @@ -6,9 +6,8 @@ def assert_results_equal( - t, pt, qt, infidelity, energy, population, T, n_osc, n_levels, sample_indices, - expected_length, expected_infidelity, expected_pt, expected_qt, expected_energy, expected_population - ): + t, pt, qt, infidelity, energy, population, T, n_osc, n_levels, sample_indices, + expected_length, expected_infidelity, expected_pt, expected_qt, expected_energy, expected_population): """ Utility function to assert that the results of quantum run match the expected values within given tolerances. """ From 29f5c3f65ccf26254e2e1c19eb30d5e0b7f36dc5 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 12:01:03 -0600 Subject: [PATCH 22/31] Add evalControls test case --- tests/python/test_evalControls.py | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/python/test_evalControls.py diff --git a/tests/python/test_evalControls.py b/tests/python/test_evalControls.py new file mode 100644 index 00000000..8cb90733 --- /dev/null +++ b/tests/python/test_evalControls.py @@ -0,0 +1,35 @@ +import numpy as np +from pytest import approx +from quandary import Quandary + + +def test_evalControls_updates_timestep(): + """ + Test that evalControls properly updates timestep (dT) to match sampling rate. + """ + + T = 5.0 + quandary = Quandary( + Ne=[2], + freq01=[4.0], + T=T, + verbose=False + ) + + original_dT = quandary.dT + original_nsteps = quandary.nsteps + + # Test evalControls with different sampling rate + points_per_ns = 2 + time, _, _ = quandary.evalControls(points_per_ns=points_per_ns) + + expected_nsteps = int(np.floor(T * points_per_ns)) + expected_dT = T / expected_nsteps + + assert time[0] == approx(0.0) + assert time[-1] == approx(T) + assert time[1] - time[0] == approx(expected_dT) + + # Verify original settings are restored + assert quandary.dT == approx(original_dT) + assert quandary.nsteps == original_nsteps From 813f27b7c06fa081bcf7c9cc7bfb98bb7fa8847c Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 15:23:45 -0600 Subject: [PATCH 23/31] Use random control with fixed random seed --- tests/python/test_example_cnot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/test_example_cnot.py b/tests/python/test_example_cnot.py index 2b582d36..6b9b6f78 100644 --- a/tests/python/test_example_cnot.py +++ b/tests/python/test_example_cnot.py @@ -137,7 +137,7 @@ def test_example_cnot(mpi_exec): T=T, targetgate=unitary, verbose=False, - randomize_init_ctrl=False, + rand_seed=1234, ) t, pt, qt, infidelity, energy, population = quandary.optimize( From 17996c338b758befe1876a160d9ce63c6cafda54 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 16:02:28 -0600 Subject: [PATCH 24/31] Update expected values for test --- tests/python/test_example_cnot.py | 82 +++++++++++++++---------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/tests/python/test_example_cnot.py b/tests/python/test_example_cnot.py index 6b9b6f78..92d67233 100644 --- a/tests/python/test_example_cnot.py +++ b/tests/python/test_example_cnot.py @@ -7,65 +7,65 @@ pytestmark = pytest.mark.regression EXPECTED_LENGTH = 1222 -EXPECTED_INFIDELITY = 9.579442993001841e-05 +EXPECTED_INFIDELITY = 8.878046657001537e-05 EXPECTED_PT = [ [ - 6.616725193332, 0.594108955114234, -4.6612041043857, -10.057008545847399, -16.7925272532101, - -4.37803267051435, 29.429289693884897, -1.99065216688873, 1.55672833280728, -8.84077714799125 + 2.87734000820681, 10.2084828702097, 3.56051692161081, 10.024110887425099, 0.119689588026969, + -0.562436390448981, -8.93345535901571, 3.44484827705617, 2.06286670752315, 3.89249555192154 ], [ - 41.115114703615404, -126.979851925522, 1.95887435527856, -44.1884354195065, 69.1903244908076, - 98.6254243074309, -72.6834065507498, -7.877713819489899, -93.5283459543858, 12.9802269197434 + -2.9180159231296896, 12.2087515999371, -23.089156431207, -4.19328393381954, -5.69791415120672, + -2.6886978940074, 11.226563801034, -5.036756985169349, 10.5696800602371, -1.62524585211734 ], ] EXPECTED_QT = [ [ - 12.2046682803128, -4.106223161934349, -4.25224002148494, 14.913352257247501, -10.267172008817, - 12.213110218227799, -7.2090372842489, -27.3356430221432, -0.0754141037646605, -14.1999385354707 + -0.348841624741643, 8.53241095717569, -4.4376504465481, 3.8174295155143803, 10.4326608744893, + -0.095498326189503, 5.25168638153997, -2.8110244912535003, -7.59465730686269, 2.47175448714252 ], [ - -42.4224419634531, 70.5220436508868, -55.3237840325884, 138.403062344659, -27.9645427362594, - -109.78387954138701, 41.9326700775612, -105.26165922349901, 115.179149640371, -3.45010594052695 + 6.427777598244551, -3.3248790239003, -4.5100523360834295, 6.087543657080141, -10.646609746997699, + 11.392012292061, -3.3794131918228403, -8.74872063545075, 7.451333086132051, -0.0766950105907232 ], ] EXPECTED_ENERGY = [ [ [ - 0.0, 0.164018720327432, 0.681155470507073, 0.91595079897646, 0.409625193180763, - 0.0451618509044243, 0.145909796953593, 0.894811107898173, 0.276174785115582, 7.18972153758039e-05 + 0.0, 0.34901327142301, 0.473011956850604, 0.555285026597096, 0.069842096071947, + 0.0494333733507312, 0.0412389950511918, 0.0449879240940497, 0.092441027177265, 8.40973352444999e-05 ], [ - 0.0, 0.148554713902497, 0.677563453874268, 0.883069457661089, 0.433062321962301, - 0.0409323679823027, 0.237968075668888, 0.937615058415591, 0.288744113780608, 6.80597196600991e-05 + 0.0, 0.329712745694427, 0.631427344995695, 0.705222483334731, 0.195021533108613, + 0.105515613439278, 0.049564530621508, 0.0440871191855571, 0.0578785086924327, 4.80529018699317e-05 ], [ - 1.0, 0.833693887189515, 0.30231425655866, 0.0781041328616079, 0.612526687583752, - 0.950165031479551, 0.785949803569589, 0.0630467958468525, 0.713636929875362, 0.9999200364916 + 1.0, 0.683972616987854, 0.513519662449962, 0.428832898801532, 0.811508875591578, + 0.899674588776469, 0.940734433368898, 0.96799101447616, 0.931984272724262, 0.99993177546283 ], [ - 1.0, 0.853732678324426, 0.338966818414533, 0.122875610552277, 0.54478579702393, - 0.963740749531197, 0.830172323495192, 0.104527037572075, 0.7214441710184, 0.99994000664992 + 1.0, 0.637301365961018, 0.382041036035193, 0.310659591977753, 0.923627496026638, + 0.94537642527642, 0.968462041771141, 0.942933942897476, 0.917696192253395, 0.999936075198039 ], ], [ [ - 0.0, 0.833380867552618, 0.211467502291388, 0.10772688588061, 0.994486567753026, - 0.332783402008406, 0.328512418616942, 0.849318770276658, 0.365928160207867, 6.3927010622781e-05 + 0.0, 0.987133111411216, 0.562433193103199, 0.769075465888702, 0.351891781635455, + 0.349514600195487, 0.377439464911376, 0.245808211210325, 0.739043033057218, 8.03904470221222e-05 ], [ - 1.0, 0.165804605873793, 0.794830994785911, 0.891785033888527, 0.00521798681585307, - 0.658576469218537, 0.682364479561092, 0.154976310427688, 0.637505155319165, 0.999947805542938 + 1.0, 0.0333701947461882, 0.408345432340385, 0.275536717225252, 0.607727573743172, + 0.65903560390506, 0.615696981888637, 0.734508095105381, 0.256558085054453, 0.999939827908553 ], [ - 0.0, 0.926141799780157, 0.636879769305457, 0.0297187946675154, 0.576532188630711, - 0.937277826669486, 0.1423287253708, 0.0620880380451264, 0.477483384114772, 0.999939141264031 + 0.0, 0.958580676348588, 0.583890977393659, 0.788362984366934, 0.433827024077578, + 0.929364744019182, 0.419775139103579, 0.871558222749287, 0.211581398264166, 0.99991222208567 ], [ - 1.0, 0.0746727268238372, 0.356821733548592, 0.970769285553542, 0.423763256939225, - 0.0713623024323497, 0.846794376113813, 0.933616880573983, 0.519083300077874, 4.91262629975248e-05 + 1.0, 0.0209160175402728, 0.445330397327355, 0.167024833245191, 0.606553620954251, + 0.0620850525500678, 0.587088414384345, 0.148125472053068, 0.792817484475808, 6.75603725578727e-05 ], ], ] @@ -73,38 +73,38 @@ EXPECTED_POPULATION = [ [ [ - 1.0, 0.835981279673114, 0.318844529497196, 0.0840492010301489, 0.590374806827084, - 0.954838149104735, 0.854090203057394, 0.105188892114705, 0.723825214898353, 0.999928102824303 + 1.0, 0.650986728594546, 0.526988043322036, 0.444714973597238, 0.930157904163221, + 0.950566626905697, 0.958761005215817, 0.955012076207555, 0.907558973131879, 0.999915902999063 ], [ - 1.0, 0.851445286098087, 0.322436546129973, 0.116930542347156, 0.566937678047111, - 0.959067632027563, 0.762031924344571, 0.0623849415993417, 0.71125588623519, 0.999931940321825 + 1.0, 0.670287254332655, 0.368572655214771, 0.294777516899832, 0.804978467167963, + 0.894484386858014, 0.950435469700482, 0.955912881149872, 0.942121491658557, 0.999951947459561 ], [ - 0.0, 0.166306112811131, 0.697685743445493, 0.921895867144501, 0.387473312423499, - 0.0498349685284324, 0.214050196441385, 0.936953204165973, 0.286363070138658, 7.99635494829092e-05 + 0.0, 0.316027383043538, 0.486480337824313, 0.571167101492937, 0.188491124757645, + 0.100325411595143, 0.0592655670333261, 0.0320089859467604, 0.0680157277087126, 6.82250014792584e-05 ], [ - 0.0, 0.146267321676212, 0.661033181589251, 0.87712438945341, 0.455214202982917, - 0.0362592504767486, 0.169827676515773, 0.89547296244067, 0.278555828995427, 5.99933849104876e-05 + 0.0, 0.362698634062056, 0.617958964216483, 0.68934040830239, 0.0763725043254758, + 0.0546235750840321, 0.031537958631141, 0.0570660575129625, 0.0823038081685347, 6.39252396400044e-05 ], ], [ [ - 1.0, 0.166619132447928, 0.788532497712881, 0.892273114125999, 0.00551343225482061, - 0.667216598000753, 0.671487581394045, 0.15068122973622, 0.634071839806068, 0.999936073029056 + 1.0, 0.0128668886063392, 0.437566807069441, 0.230924534305633, 0.648108218599712, + 0.650485400060942, 0.622560535355633, 0.75419178909128, 0.260956967251926, 0.999919609887286 ], [ - 0.0, 0.834195394126791, 0.20516900521833, 0.108214966119718, 0.994782013193558, - 0.341423530791329, 0.317635520452367, 0.845023689587245, 0.362494844696633, 5.21944985474435e-05 + 0.0, 0.966629805280894, 0.591654567870081, 0.724463283009311, 0.392272426533404, + 0.340964396392231, 0.384303018433353, 0.265491905230048, 0.743441915296537, 6.01724528776744e-05 ], [ - 1.0, 0.0738582002204882, 0.363120230698696, 0.970281205338593, 0.423467811376541, - 0.0627221733384974, 0.857671274640174, 0.937911961967699, 0.522516615899248, 6.08587770516464e-05 + 1.0, 0.0414193236828037, 0.416109022880615, 0.211637015927536, 0.566172976271645, + 0.0706352563524305, 0.580224861298645, 0.128441777673634, 0.788418602168809, 8.7778378639303e-05 ], [ - 0.0, 0.925327273176801, 0.643178266455191, 0.0292307144521444, 0.576236743067622, - 0.928637697575596, 0.153205623897152, 0.0663831194387622, 0.480916699935952, 0.999950873771833 + 0.0, 0.979083982482801, 0.554669602924322, 0.832975167034952, 0.393446379397863, + 0.937914947810385, 0.412911586017937, 0.851874528357371, 0.207182515946122, 0.999932440065121 ], ], ] From e141bf81dd50cad5e937ef6f1235f2abdfeb19a8 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 16:18:21 -0600 Subject: [PATCH 25/31] Use temporary path for test output --- tests/python/test_example_cnot.py | 7 +++++-- tests/python/test_example_cnot_withguardlevels.py | 7 +++++-- tests/python/test_example_piecewise_constant_controls.py | 7 +++++-- tests/python/test_example_qft.py | 7 +++++-- tests/python/test_example_spinchain.py | 5 ++++- tests/python/test_example_state_to_state.py | 7 +++++-- tests/python/test_example_swap02.py | 7 +++++-- tests/python/test_example_swap12.py | 7 +++++-- 8 files changed, 39 insertions(+), 15 deletions(-) diff --git a/tests/python/test_example_cnot.py b/tests/python/test_example_cnot.py index 92d67233..74371c8c 100644 --- a/tests/python/test_example_cnot.py +++ b/tests/python/test_example_cnot.py @@ -1,3 +1,4 @@ +import os import pytest import numpy as np from quandary import Quandary @@ -114,8 +115,9 @@ SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] -def test_example_cnot(mpi_exec): +def test_example_cnot(mpi_exec, tmp_path, request): """Test CNOT gate optimization using Python interface.""" + datadir_path = os.path.join(tmp_path, request.node.name) freq01 = [4.80595, 4.8601] favg = sum(freq01)/len(freq01) @@ -142,7 +144,8 @@ def test_example_cnot(mpi_exec): t, pt, qt, infidelity, energy, population = quandary.optimize( mpi_exec=mpi_exec, - maxcores=2 + maxcores=2, + datadir=datadir_path, ) assert_results_equal( diff --git a/tests/python/test_example_cnot_withguardlevels.py b/tests/python/test_example_cnot_withguardlevels.py index 975959de..3e1783b9 100644 --- a/tests/python/test_example_cnot_withguardlevels.py +++ b/tests/python/test_example_cnot_withguardlevels.py @@ -1,3 +1,4 @@ +import os import pytest import numpy as np from quandary import Quandary @@ -114,8 +115,9 @@ SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] -def test_example_cnot_withguardlevels(mpi_exec): +def test_example_cnot_withguardlevels(mpi_exec, tmp_path, request): """Test CNOT gate optimization with guard levels using Python interface.""" + datadir_path = os.path.join(tmp_path, request.node.name) Ne = [2, 2] Ng = [2, 2] @@ -159,7 +161,8 @@ def test_example_cnot_withguardlevels(mpi_exec): t, pt, qt, infidelity, energy, population = quandary.optimize( mpi_exec=mpi_exec, - maxcores=4 + maxcores=4, + datadir=datadir_path, ) assert_results_equal( diff --git a/tests/python/test_example_piecewise_constant_controls.py b/tests/python/test_example_piecewise_constant_controls.py index 079b7653..ca839cf2 100644 --- a/tests/python/test_example_piecewise_constant_controls.py +++ b/tests/python/test_example_piecewise_constant_controls.py @@ -1,3 +1,4 @@ +import os import pytest import numpy as np from quandary import Quandary @@ -114,8 +115,9 @@ SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] -def test_example_piecewise_constant_controls(mpi_exec): +def test_example_piecewise_constant_controls(mpi_exec, tmp_path, request): """Test CNOT gate optimization with piecewise constant controls using Python interface.""" + datadir_path = os.path.join(tmp_path, request.node.name) freq01 = [4.80595, 4.8601] Jkl = [0.005] @@ -153,7 +155,8 @@ def test_example_piecewise_constant_controls(mpi_exec): t, pt, qt, infidelity, energy, population = quandary.optimize( mpi_exec=mpi_exec, - maxcores=1 + maxcores=1, + datadir=datadir_path, ) assert_results_equal( diff --git a/tests/python/test_example_qft.py b/tests/python/test_example_qft.py index c92a79f7..c6f00358 100644 --- a/tests/python/test_example_qft.py +++ b/tests/python/test_example_qft.py @@ -1,3 +1,4 @@ +import os import pytest import numpy as np from quandary import Quandary @@ -266,8 +267,9 @@ def get_QFT_gate(dim): return gate_Hd -def test_example_qft(mpi_exec): +def test_example_qft(mpi_exec, tmp_path, request): """Test 3-qubit QFT gate optimization using Python interface.""" + datadir_path = os.path.join(tmp_path, request.node.name) # Parameters for 3-qubit case nqubits = 3 @@ -333,7 +335,8 @@ def test_example_qft(mpi_exec): t, pt, qt, infidelity, energy, population = quandary.optimize( mpi_exec=mpi_exec, - maxcores=4 + maxcores=4, + datadir=datadir_path, ) assert_results_equal( diff --git a/tests/python/test_example_spinchain.py b/tests/python/test_example_spinchain.py index 72245073..ae72acf7 100644 --- a/tests/python/test_example_spinchain.py +++ b/tests/python/test_example_spinchain.py @@ -1,3 +1,4 @@ +import os import pytest import numpy as np from quandary import Quandary @@ -149,8 +150,9 @@ def mapCoeffs_SpinChainToQuandary(N: int, h: list, U: list, J: list): return freq01, crosskerr, Jkl -def test_example_spinchain(mpi_exec): +def test_example_spinchain(mpi_exec, tmp_path, request): """Test spin chain simulation using Python interface.""" + datadir_path = os.path.join(tmp_path, request.node.name) N = 8 U_amp = 1.0 @@ -198,6 +200,7 @@ def test_example_spinchain(mpi_exec): t, pt, qt, infidelity, energy, population = quandary.simulate( mpi_exec=mpi_exec, maxcores=1, + datadir=datadir_path, ) assert_results_equal( diff --git a/tests/python/test_example_state_to_state.py b/tests/python/test_example_state_to_state.py index 309b2dfc..1485e252 100644 --- a/tests/python/test_example_state_to_state.py +++ b/tests/python/test_example_state_to_state.py @@ -1,3 +1,4 @@ +import os import pytest import numpy as np from quandary import Quandary @@ -46,8 +47,9 @@ SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] -def test_example_state_to_state(mpi_exec): +def test_example_state_to_state(mpi_exec, tmp_path, request): """Test state-to-state preparation using Python interface.""" + datadir_path = os.path.join(tmp_path, request.node.name) Ne = [2] Ng = [1] @@ -76,7 +78,8 @@ def test_example_state_to_state(mpi_exec): t, pt, qt, infidelity, energy, population = quandary.optimize( mpi_exec=mpi_exec, - maxcores=1 + maxcores=1, + datadir=datadir_path, ) assert_results_equal( diff --git a/tests/python/test_example_swap02.py b/tests/python/test_example_swap02.py index 3c341716..c0446b56 100644 --- a/tests/python/test_example_swap02.py +++ b/tests/python/test_example_swap02.py @@ -1,3 +1,4 @@ +import os import pytest from quandary import Quandary from utils import assert_results_equal @@ -61,8 +62,9 @@ SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] -def test_example_swap02(mpi_exec): +def test_example_swap02(mpi_exec, tmp_path, request): """Test SWAP 0-2 gate optimization using Python interface.""" + datadir_path = os.path.join(tmp_path, request.node.name) Ne = [3] Ng = [1] @@ -88,7 +90,8 @@ def test_example_swap02(mpi_exec): t, pt, qt, infidelity, energy, population = quandary.optimize( mpi_exec=mpi_exec, - maxcores=4 + maxcores=4, + datadir=datadir_path, ) assert_results_equal( diff --git a/tests/python/test_example_swap12.py b/tests/python/test_example_swap12.py index 1c11cd18..dc050df4 100644 --- a/tests/python/test_example_swap12.py +++ b/tests/python/test_example_swap12.py @@ -1,3 +1,4 @@ +import os import pytest import numpy as np from quandary import Quandary @@ -114,8 +115,9 @@ SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] -def test_example_swap12(mpi_exec): +def test_example_swap12(mpi_exec, tmp_path, request): """Test SWAP 1-2 gate optimization using Python interface.""" + datadir_path = os.path.join(tmp_path, request.node.name) freq01 = [5.12, 5.06] Jkl = [5.0e-3] @@ -144,7 +146,8 @@ def test_example_swap12(mpi_exec): t, pt, qt, infidelity, energy, population = quandary.optimize( mpi_exec=mpi_exec, - maxcores=4 + maxcores=4, + datadir=datadir_path, ) assert_results_equal( From 477a14f361eea0b793afe2079404a0130f12fe70 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 16:23:31 -0600 Subject: [PATCH 26/31] Rename variable --- tests/python/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/python/utils.py b/tests/python/utils.py index 1fd53832..ca649032 100644 --- a/tests/python/utils.py +++ b/tests/python/utils.py @@ -41,7 +41,7 @@ def assert_results_equal( np.testing.assert_allclose(pop_samples, expected_population[i][j], rtol=REL_TOL, atol=ABS_TOL) -def print_expected_values(infidelity, pt, qt, expectedEnergy, population, sample_indices, n_osc): +def print_expected_values(infidelity, pt, qt, energy, population, sample_indices, n_osc): """ Utility function to print actual values in the format needed for EXPECTED arrays. Call this function with actual test results to get copy-pasteable expected values. @@ -83,8 +83,8 @@ def print_expected_values(infidelity, pt, qt, expectedEnergy, population, sample print("EXPECTED_ENERGY = [") for i in range(n_osc): print(" [") - for j in range(len(expectedEnergy[i])): - energy_data = expectedEnergy[i][j] + for j in range(len(energy[i])): + energy_data = energy[i][j] energy_samples = [energy_data[idx] for idx in sample_indices] print(" [") for k in range(0, len(energy_samples), 5): From dd835e9f13dd24692eed8f184b03e6b1651ddc5b Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 16:24:08 -0600 Subject: [PATCH 27/31] Update expected values --- ...est_example_piecewise_constant_controls.py | 83 +++---- tests/python/test_example_qft.py | 218 +++++++++--------- tests/python/test_example_state_to_state.py | 18 +- tests/python/test_example_swap02.py | 34 +-- tests/python/test_example_swap12.py | 82 +++---- 5 files changed, 218 insertions(+), 217 deletions(-) diff --git a/tests/python/test_example_piecewise_constant_controls.py b/tests/python/test_example_piecewise_constant_controls.py index ca839cf2..13802028 100644 --- a/tests/python/test_example_piecewise_constant_controls.py +++ b/tests/python/test_example_piecewise_constant_controls.py @@ -4,69 +4,70 @@ from quandary import Quandary from utils import assert_results_equal + # Mark all tests in this file as regression tests pytestmark = pytest.mark.regression EXPECTED_LENGTH = 1222 -EXPECTED_INFIDELITY = 9.257378541704941e-05 +EXPECTED_INFIDELITY = 9.967903512697607e-05 EXPECTED_PT = [ [ - 1.39139720094627, -7.252599652352569, 4.47706380946171, 1.4726647949197, -1.19913109722706, - -0.33927983009720397, 2.3595839470663598, 0.677966851416258, -7.29939358795371, 1.8307265770282999 + -1.1455758286673898, 7.30666099687124, -1.56254374996937, -2.36557441557185, 1.45983158306447, + 1.1348196056112798, 0.17027119825244802, 6.66935674855051, 3.83949098703669, -1.29770951519605 ], [ - -0.0733648695919313, -1.8066964117515698, 6.68654354551804, -7.48223143066535, 5.13340197845506, - 0.315835131695272, -3.73285646771321, 3.9510316874371703, -1.33023216001612, 0.124959893691128 + -0.0857364353164705, 0.26682652763546205, -2.84259629787855, 5.8730580454732495, -7.71845217474982, + 4.108702767526379, 4.31045393001853, -8.29308842920559, 2.51709250120214, 0.100284940848674 ], ] EXPECTED_QT = [ [ - -0.196580437889947, -4.8278406547759305, 3.6663142384181198, -1.80095432726682, 2.0377228566350696, - -1.97182393857655, 1.80555521627317, -3.32011281124973, 5.250899308856639, -0.229292501560434 + -0.102518227951347, 8.03543317276026, -4.12672901503846, -1.84670226508142, -2.3095502994813, + 2.02088269375347, -3.66417041158405, -0.41116620626056, 0.40459736675858904, 0.226771921733744 ], [ - -0.600034770502575, 0.688006184634379, 1.84509450260025, -0.24432814453945298, -2.6551546671386603, - 7.474760031265401, -5.53602372411908, 1.5594733148807, -0.347284269350974, 0.043548086484513006 + 0.0852976327317638, -0.30191635506298403, 1.15730296070651, 1.98832750472986, 1.29849602408214, + -7.59955466164568, 7.1300223152945605, -5.95519648260564, 2.36244366579429, -0.3462938582071 ], ] EXPECTED_ENERGY = [ [ [ - 0.0, 0.0699185568731366, 0.182704095177448, 0.249731600013859, 0.470426559112901, - 0.447394659045756, 0.224091290602656, 0.0779512718954046, 0.0650377137298774, 6.22310457717102e-05 + 0.0, 0.00370220612622186, 0.0801332542644481, 0.15688463267758, 0.548903485801343, + 0.605435971064497, 0.588636450251953, 0.600128445340578, 0.171512211227719, 4.78498051761215e-05 ], [ - 0.0, 0.0930391138135943, 0.23743892043364, 0.413114574406591, 0.550666725329946, - 0.459127111006311, 0.334506254691026, 0.144006964319411, 0.0876700328696583, 5.11545031070254e-05 + 0.0, 0.0249069977367134, 0.094112746268439, 0.210177591590367, 0.613751525451786, + 0.551293437285607, 0.627396931858309, 0.755186728733611, 0.201908774636426, 7.46873419305587e-05 ], [ - 1.0, 0.905603791137994, 0.78486272542267, 0.726997485982308, 0.579209752236972, - 0.537878098046528, 0.671699851041516, 0.898618730017578, 0.933934688944795, 0.999917511582725 + 1.0, 0.972984763890452, 0.887143848728789, 0.794289503333496, 0.443574516832106, + 0.416077263717592, 0.326537124922318, 0.257799553542498, 0.82670168506467, 0.999943362518778 ], [ - 1.0, 0.931438538247787, 0.794994259093699, 0.610156339798125, 0.399696963538377, - 0.555600132161797, 0.769702603924927, 0.879423033910962, 0.913357564385493, 0.999969102986179 + 1.0, 0.998406032199003, 0.938610150832428, 0.838648272670286, 0.393770472430779, + 0.427193328442698, 0.457429493477111, 0.386885273563193, 0.799877329825436, 0.999934100640022 ], ], [ [ - 0.0, 0.0139344633082164, 0.171765683822903, 0.549172717868476, 0.50363006362385, - 0.311552152123449, 0.48754998982979, 0.249155258712721, 0.0145811031242011, 4.78606077574857e-05 + 0.0, 0.00210990545133481, 0.00704882244254717, 0.244548352176713, 0.553119340084485, + 0.321745694464255, 0.477150970394771, 0.561363914356036, 0.0615657334786886, 8.22413835454486e-05 ], [ - 1.0, 0.966726525551247, 0.819807830385754, 0.480870264221943, 0.509512169840825, - 0.673725087146305, 0.525882005326489, 0.740409351672592, 0.964210651043143, 0.999983363807341 + 1.0, 0.974117273615089, 0.954893484094794, 0.747556963093925, 0.451943034138268, + 0.672297699308123, 0.517590897628196, 0.43531927925783, 0.922195291055279, 0.999899924632047 ], [ - 0.0, 0.0539215132513357, 0.143866436910906, 0.382895489666004, 0.614576581108313, - 0.596135139899607, 0.633239241495552, 0.91162937655105, 0.984631303988097, 0.999934242114831 + 0.0, 0.072458882749064, 0.187064188322364, 0.329675230692441, 0.540287018259676, + 0.648822763889229, 0.379007502887368, 0.597276056588351, 0.980989706070835, 0.99992765780649 ], [ - 1.0, 0.965417497890465, 0.864560048886911, 0.587061528101101, 0.372281185307642, - 0.418587620841731, 0.353328763450195, 0.0988060135274491, 0.03657694219604, 3.4533577363965e-05 + 1.0, 0.951313938373316, 0.850993505339821, 0.678219454514544, 0.45465060808428, + 0.357133842978274, 0.626250630042886, 0.4060407503267, 0.0352492695595698, 9.01763846159662e-05 ], ], ] @@ -74,38 +75,38 @@ EXPECTED_POPULATION = [ [ [ - 1.0, 0.930081443141761, 0.817295904842303, 0.75026840001086, 0.529573440922835, - 0.552605340998218, 0.775908709447833, 0.922048728161235, 0.934962286333544, 0.999937769021251 + 1.0, 0.996297793906866, 0.919866745769127, 0.843115367467209, 0.451096514349396, + 0.394564029095284, 0.41136354991753, 0.399871554840764, 0.828487788975188, 0.999952150419256 ], [ - 1.0, 0.90696088618882, 0.762561079569985, 0.586885425621664, 0.449333274709712, - 0.540872889042223, 0.665493745364733, 0.855993035741032, 0.912329967191519, 0.999948845559214 + 1.0, 0.975093002264432, 0.905887253741386, 0.789822408421996, 0.386248474568972, + 0.448706562744717, 0.372603068181163, 0.244813271324819, 0.798091225434512, 0.9999253127422 ], [ - 0.0, 0.0943962088645299, 0.215137274581816, 0.273002514036944, 0.420790247791917, - 0.462121901990549, 0.328300149001861, 0.10138127003291, 0.0660653111133352, 8.24884791431685e-05 + 0.0, 0.027015236110399, 0.112856151311767, 0.205710496725285, 0.556425483232299, + 0.583922736355513, 0.673462875159617, 0.742200446549557, 0.173298315048093, 5.66376102744832e-05 ], [ - 0.0, 0.0685614617674683, 0.205005740928792, 0.389843660228178, 0.600303036498988, - 0.444399867883395, 0.230297396125311, 0.120576966142488, 0.0866424356698608, 3.08970701760464e-05 + 0.0, 0.00159396784041352, 0.0613898492118107, 0.161351727426722, 0.6062295276719, + 0.572806671669023, 0.542570506643452, 0.613114726569719, 0.200122670317697, 6.58995172308447e-05 ], ], [ [ - 1.0, 0.986065536706681, 0.828234316196848, 0.450827282156242, 0.496369936411885, - 0.688447847920526, 0.512450010220699, 0.750844741343919, 0.98541889693922, 0.999952139459265 + 1.0, 0.997890094581753, 0.992951177591028, 0.755451647968075, 0.446880660066254, + 0.678254305695527, 0.522849029774711, 0.438636085825306, 0.938434266724218, 0.999917758840887 ], [ - 0.0, 0.0332734744511672, 0.180192169617871, 0.519129735806312, 0.490487830198833, - 0.326274912902229, 0.47411799472927, 0.259590648387851, 0.0357893490180342, 1.66362549805352e-05 + 0.0, 0.0258827263860563, 0.0451065159150301, 0.252443036918438, 0.548056965882489, + 0.327702300722201, 0.482409102411276, 0.5646807208006, 0.0778047090156592, 0.000100075452083218 ], [ - 1.0, 0.946078486751189, 0.85613356309358, 0.617104510353248, 0.385423418920577, - 0.403864860137471, 0.366760758547825, 0.0883706234994385, 0.0153686960700334, 6.57579470371429e-05 + 1.0, 0.927541117251786, 0.812935811718192, 0.670324769366341, 0.459712981804729, + 0.351177236183876, 0.620992497194567, 0.402723943503705, 0.0190102940419282, 7.23423225620632e-05 ], [ - 0.0, 0.0345825021247907, 0.13543995113558, 0.412938471925202, 0.627718814729723, - 0.581412379203461, 0.646671236600043, 0.901193986526, 0.963423057859314, 0.999965466478991 + 0.0, 0.0486860616661003, 0.149006494704418, 0.321780545582464, 0.545349392018398, + 0.642866157133447, 0.373749370077677, 0.593959249806213, 0.964750730583563, 0.999909823772636 ], ], ] diff --git a/tests/python/test_example_qft.py b/tests/python/test_example_qft.py index c6f00358..40629e45 100644 --- a/tests/python/test_example_qft.py +++ b/tests/python/test_example_qft.py @@ -8,139 +8,139 @@ pytestmark = pytest.mark.regression EXPECTED_LENGTH = 3962 -EXPECTED_INFIDELITY = 0.8100341641826 +EXPECTED_INFIDELITY = 0.807955384551349 EXPECTED_PT = [ [ - 1.89222063059343, 0.117633784279222, 4.64028300872995, -0.0330222256817514, 0.126474004266131, - 2.30707289881048, -1.4716574716800102, -0.64754937225632, 2.0870897477143897, -3.5993747820438102 + 2.2913650856245997, 9.09644904851116, -17.2909860740465, 3.14875586899579, 7.8469022267136195, + -9.39852098244228, 4.5281042855394, 14.503322577238201, -9.821651366661971, 1.2879117017857 ], [ - 1.52517917625596, -2.0819686425195902, -6.16449162597314, -0.281926172015322, -2.29082646991941, - -1.35446004376719, -1.73027004354096, -1.6977449891882, -0.957325552016711, -0.66032285194334 + -13.2263008296231, -12.709094348057901, -15.810963798316703, -16.6092338221892, -13.841545820333401, + -15.2070809621358, -13.865376040249501, -16.2193455601315, -11.9046288731911, -15.325493942116301 ], [ - 3.38869388629721, 2.46448942091541, -5.18990161779653, 3.23625763732884, 0.21172074728456902, - -0.5306089302627199, 0.41489476880584397, -1.3511518437146701, 4.63365073411168, -3.2543080614975404 + 8.50394099306291, -1.66446338967996, -18.6047215591729, 16.2793069480132, -15.4167928165748, + -3.06716987564059, 15.4140895082908, -14.3948359344439, -6.5420138844972, 8.27928099844133 ], ] EXPECTED_QT = [ [ - 4.282471243949811, -1.90435084775161, 2.4105421852599003, 0.943664506103648, -1.48246991300687, - 1.32987551448728, -0.892631152337576, -2.95742646747671, -0.0736760483790147, -0.506870586543159 + -10.3055805688327, 18.547996566386, 5.99633788520833, -14.7492986560445, 9.38690829174289, + 10.765519868011, -16.3024877831227, 10.0457369314909, 11.435907408410602, -8.659789931837421 ], [ - 0.58274688212878, -0.5695945929016201, 0.777803006125851, -0.49131203698567594, -1.17510621256011, - 0.189918970015857, 1.79672837073849, 2.5547196576333, 1.96618771070137, 0.334327992279328 + -6.552844176227279, -14.546229060653001, -12.861344232317801, -14.4594275843637, -14.9531441603284, + -12.7940664307229, -15.0427619520243, -10.5511177723819, -15.4727302435704, -13.9479688836996 ], [ - 5.5996176445134695, -0.573714754569928, 3.04724887495592, 1.71942470756566, -0.877046652546983, - 1.3325727744462101, -3.90923703013145, 2.14761212417968, -0.288156432691881, -1.83963133335511 + 11.3131165019958, -19.4150069695826, 9.08858885239631, 5.89552316335488, -13.3352586995519, + 20.585711340520103, -7.00086614252265, -13.1050128906131, 14.8338178835344, -7.261344243272879 ], ] EXPECTED_ENERGY = [ [ [ - 0.0, 0.0148607579341863, 0.116727716291541, 0.345569094665688, 0.420492333922264, - 0.502613489759601, 0.762594113234293, 0.785381772060781, 0.839275883284673, 0.929013124070048 + 0.0, 0.871740174140779, 0.490091064288099, 0.186335216229364, 0.918292737656804, + 0.478273494645005, 0.11251939507393, 0.879350927167369, 0.426241674349888, 0.199305858262601 ], [ - 0.0, 0.0146067840666606, 0.11754798689117, 0.346519320469488, 0.4183177982045, - 0.50464640883737, 0.762920218593243, 0.784725458623699, 0.84231160354798, 0.928128544837562 + 0.0, 0.868360011032445, 0.490963305110186, 0.194038255724569, 0.916298721789158, + 0.481047465799636, 0.104806663549212, 0.877519131294704, 0.407037999087305, 0.206386987583373 ], [ - 0.0, 0.0338745510923139, 0.148104595599986, 0.261474185761009, 0.43195551641555, - 0.542510481056473, 0.662377107041912, 0.773081817363954, 0.845641427818187, 0.836865832278204 + 0.0, 0.899063477700849, 0.323022026136045, 0.318633068484335, 0.955121439680414, + 0.497262619342675, 0.145864268410915, 0.958173602970953, 0.240135682686225, 0.244528237039347 ], [ - 0.0, 0.0341240967106616, 0.147546776276661, 0.258858419511221, 0.43301547251873, - 0.539199965870178, 0.659097143189277, 0.77199876021302, 0.842398924439871, 0.836306522946996 + 0.0, 0.902179316365234, 0.318345594178397, 0.317004163022575, 0.953988105812139, + 0.494938678281903, 0.143809016498184, 0.960460829452475, 0.265956898465207, 0.247684742825489 ], [ - 1.0, 0.965748235524499, 0.865577990181392, 0.656382457528953, 0.570472972368594, - 0.492800248880717, 0.25044370810636, 0.216403809620927, 0.161196371800905, 0.0904069492983516 + 1.0, 0.14764300905675, 0.510666947446306, 0.815969003998871, 0.0790755210886338, + 0.493391671461912, 0.915056851633572, 0.142616896234222, 0.574789185470027, 0.804076761314804 ], [ - 1.0, 0.966339411555136, 0.864690415748644, 0.655340165309916, 0.572787651114863, - 0.491244274146324, 0.250766977888323, 0.217115378257751, 0.158045158422749, 0.0914413145926092 + 1.0, 0.151071767963378, 0.51247545346474, 0.808589659469236, 0.0779212899125423, + 0.494734774948987, 0.924343214815746, 0.130848538865006, 0.625308236472721, 0.770151457175104 ], [ - 1.0, 0.985302944171115, 0.869398388325261, 0.736749605523319, 0.57695016774174, - 0.462035000315518, 0.324900968404589, 0.224944666181529, 0.153872876896255, 0.144029544672393 + 1.0, 0.0816503881066301, 0.677252537237086, 0.678498790296523, 0.0496832185589613, + 0.531844530532734, 0.822212880788712, 0.0264601411450504, 0.751644146257915, 0.758435140441681 ], [ - 1.0, 0.98514321894768, 0.870406130707427, 0.739106751295271, 0.576008087756858, - 0.46495013117886, 0.326899763623054, 0.226348337749972, 0.157257753822829, 0.14380816732843 + 1.0, 0.078291855616928, 0.677183071949085, 0.680931843041985, 0.0496189655503093, + 0.528506764899685, 0.831387709299596, 0.0245699327764925, 0.708886177170138, 0.769430815578887 ], ], [ [ - 0.0, 0.0150176314942745, 0.0952237642656534, 0.231161654602016, 0.398633896748728, - 0.574531626678352, 0.606904878512938, 0.691463508739308, 0.71823660153979, 0.689891450405871 + 0.0, 0.977188871268564, 0.0753136468741463, 0.785518165609952, 0.247362238751697, + 0.76930486909517, 0.239006311723343, 0.77277409818515, 0.347981231583044, 0.510833957680721 ], [ - 0.0, 0.0436936513275708, 0.0900374154017835, 0.274075384483282, 0.45593919442545, - 0.519637414444859, 0.675443884172963, 0.714424686347848, 0.751882030138024, 0.682769582193614 + 0.0, 0.974429659879698, 0.135185817401484, 0.807626448408114, 0.241176799548327, + 0.767333135072996, 0.376586787556682, 0.682200961517894, 0.345437106620918, 0.32903061813537 ], [ - 1.0, 0.945930049751258, 0.872572104725977, 0.772611799325123, 0.59459094265588, - 0.414239449304858, 0.412912804143054, 0.309664967276825, 0.279371139275707, 0.32776261067736 + 1.0, 0.0420102449329318, 0.902850597487805, 0.225829286901242, 0.770261717521361, + 0.185748506284338, 0.732179741311534, 0.257131124274444, 0.683330099539097, 0.479630861974819 ], [ - 1.0, 0.957353693732795, 0.911385175111489, 0.728366698965441, 0.54374940635324, - 0.486344371457795, 0.339151646649223, 0.296283883722812, 0.25715846725817, 0.343462105862045 + 1.0, 0.0364095857597314, 0.917648485515454, 0.169971013985435, 0.736515730426277, + 0.25963938501678, 0.6244576321643, 0.336560874057442, 0.651647048494323, 0.66823099065104 ], [ - 0.0, 0.0363821119988847, 0.0780954049938329, 0.280451133940982, 0.373900998934923, - 0.510075868735727, 0.643999882919365, 0.636071542458841, 0.647625442555183, 0.708855105449523 + 0.0, 0.914862552678925, 0.139733744572265, 0.82336832359171, 0.238998197045158, + 0.692479177372269, 0.294083535463614, 0.511231241859868, 0.587026929167996, 0.433871163514347 ], [ - 0.0, 0.0618240158469952, 0.0785064319332916, 0.329018008285977, 0.422648586637927, - 0.463065811003479, 0.717857009780474, 0.657369931484991, 0.692081777543554, 0.702806510604441 + 0.0, 0.89999110551123, 0.225023134357934, 0.802416197813807, 0.259123086107682, + 0.61965751949498, 0.493305500733555, 0.361185260609536, 0.619345042550373, 0.337072109119454 ], [ - 1.0, 0.963098590451382, 0.919650029375563, 0.717591526724565, 0.627551892694819, - 0.483502258280923, 0.342530133643125, 0.353977730515162, 0.342748871896153, 0.264516686473132 + 1.0, 0.077139398614789, 0.816945249444237, 0.229564995560835, 0.757607917751682, + 0.311978931433563, 0.687837074215795, 0.483059072672636, 0.400495012795489, 0.526918937718937 ], [ - 1.0, 0.976700255412085, 0.954529674217841, 0.666723793644168, 0.582985081535942, - 0.548603200112124, 0.261199760145823, 0.340743749394855, 0.310895669814159, 0.279935948363 + 1.0, 0.0779685814039652, 0.787299324292812, 0.155705568011388, 0.748954312917913, + 0.393858476192421, 0.552543416841096, 0.595857366994724, 0.364737529323144, 0.714411361133778 ], ], [ [ - 0.0, 0.0655588575717349, 0.0953666127615174, 0.228383925020124, 0.348498151711691, - 0.273428303968501, 0.177039919128772, 0.0854203065177286, 0.0111626845277558, 0.0868589774774573 + 0.0, 0.932657043180154, 0.232883300755667, 0.499263175604497, 0.79968930129883, + 0.102079111121508, 0.965715219154716, 0.116459949551682, 0.608244244968234, 0.74496716938059 ], [ - 1.0, 0.914929104046163, 0.88771048417256, 0.772436424598468, 0.646985777288844, - 0.717671315173012, 0.828004337117799, 0.908248609385593, 0.984278193414155, 0.913728012465152 + 1.0, 0.0713176915860124, 0.741923261556371, 0.511937357716811, 0.1956573619347, + 0.904381403283237, 0.0420906771863643, 0.865130771206598, 0.389968800870834, 0.275807837340239 ], [ - 0.0, 0.0705031148232542, 0.13164941611602, 0.19830438969151, 0.311200364229386, - 0.356795065695722, 0.132688623811622, 0.107821000329134, 0.0208808765191582, 0.134883061129968 + 0.0, 0.938367269929136, 0.188086307325669, 0.397558654525151, 0.878665574429945, + 0.035477294772375, 0.982577775588476, 0.0863745341984703, 0.664278630441252, 0.78700594995822 ], [ - 1.0, 0.948810126439896, 0.88510263569838, 0.801095701928753, 0.693256304101845, - 0.652049554109235, 0.862659139829962, 0.898626752171462, 0.983648171135189, 0.864925552983935 + 1.0, 0.0578831831360669, 0.836313282585613, 0.591534704381165, 0.127699434832128, + 0.957153914724507, 0.0128037158814758, 0.932879023098306, 0.339150694690868, 0.189732920980029 ], [ - 0.0, 0.0641308638686701, 0.0974321828809197, 0.229940677967645, 0.346693827090613, - 0.275634938859907, 0.179608035901328, 0.0850605815749401, 0.0114112298956469, 0.0880961143337944 + 0.0, 0.936023410018913, 0.222175343086111, 0.512378498076051, 0.779638804575969, + 0.10806737742764, 0.957417442402706, 0.107276160185518, 0.664606612875009, 0.676172446779003 ], [ - 1.0, 0.91662946844843, 0.885670860009623, 0.770859829069763, 0.649054297157375, - 0.716214484534058, 0.826309111174828, 0.908928466794254, 0.984230209711413, 0.91312708089424 + 1.0, 0.0697192670746152, 0.754131769563904, 0.490502553267254, 0.222104047704744, + 0.898566676466265, 0.0449883020813609, 0.855134130491501, 0.334282555387071, 0.318455944791095 ], [ - 0.0, 0.0716084146957608, 0.129827595084711, 0.1957134832151, 0.31075703788224, - 0.35341587787003, 0.128296077572829, 0.106742758530496, 0.0194842289805046, 0.135561378058802 + 0.0, 0.931248886581956, 0.198395429426979, 0.378098323979276, 0.897130271416472, + 0.0222016935060309, 0.975155329212423, 0.115650833098016, 0.616705049724902, 0.852301481117069 ], [ - 1.0, 0.947830050103069, 0.887240213265323, 0.803265568539764, 0.69355424058736, - 0.654790459802924, 0.86539475548002, 0.899151524732232, 0.98490440578269, 0.862819822619949 + 1.0, 0.0627832485535767, 0.826091305744087, 0.618726732494471, 0.0994152037367946, + 0.972072528745459, 0.019251538612982, 0.921094598048128, 0.382763411337989, 0.155556249655697 ], ], ] @@ -148,104 +148,104 @@ EXPECTED_POPULATION = [ [ [ - 1.0, 0.985139242076125, 0.883272283718809, 0.654430905344746, 0.579507666088186, - 0.497386510250861, 0.237405886776199, 0.214618227949722, 0.160724116725837, 0.0709868759404779 + 1.0, 0.128259825860636, 0.50990893571468, 0.813664783774544, 0.0817072623480035, + 0.521726505360825, 0.887480604932567, 0.12064907283988, 0.573758325658211, 0.800694141745891 ], [ - 1.0, 0.98539321593334, 0.88245201310884, 0.65348067953056, 0.581682201795565, - 0.495353591162707, 0.237079781406884, 0.215274541376461, 0.157688396452205, 0.0718714551627529 + 1.0, 0.131639988968481, 0.509036694891598, 0.80596174427779, 0.0837012782140547, + 0.518952534204549, 0.895193336455928, 0.12248086871159, 0.592962000920103, 0.793613012425036 ], [ - 1.0, 0.96612544890773, 0.851895404400098, 0.738525814239158, 0.568044483584635, - 0.457489518943723, 0.337622892958313, 0.226918182636287, 0.15435857218206, 0.163134167722058 + 1.0, 0.100936522300607, 0.676977973866748, 0.681366931519649, 0.0448785603245856, + 0.502737380663259, 0.854135731595909, 0.0418263970366679, 0.759864317322149, 0.755471762969313 ], [ - 1.0, 0.965875903289341, 0.852453223723351, 0.741141580488827, 0.566984527481334, - 0.460800034129898, 0.340902856810848, 0.228001239787137, 0.157601075560305, 0.163693477053312 + 1.0, 0.0978206836357321, 0.681654405823485, 0.682995836979635, 0.0460118941908266, + 0.505061321721877, 0.856190983506639, 0.0395391705534387, 0.734043101541823, 0.75231525718272 ], [ - 0.0, 0.034251764475504, 0.134422009818618, 0.343617542471092, 0.429527027631466, - 0.507199751119354, 0.749556291893752, 0.783596190379213, 0.838803628199257, 0.90959305070191 + 0.0, 0.852356990944198, 0.489333052555531, 0.184030996003463, 0.920924478914508, + 0.50660832854214, 0.0849431483716364, 0.857383103772003, 0.425210814537241, 0.195923238693364 ], [ - 0.0, 0.0336605884449191, 0.135309584251455, 0.344659834690291, 0.427212348885368, - 0.50875572585392, 0.749233022111954, 0.782884621742538, 0.841954841577546, 0.908558685407704 + 0.0, 0.848928232038026, 0.487524546537911, 0.191410340534224, 0.922078710091827, + 0.505265225056285, 0.0756567851902375, 0.86915146114167, 0.374691763534784, 0.229848542832694 ], [ - 0.0, 0.0146970558288883, 0.13060161167475, 0.263250394476727, 0.423049832258321, - 0.537964999684555, 0.675099031595522, 0.775055333818613, 0.846127123103906, 0.855970455327864 + 0.0, 0.918349611894318, 0.32274746276484, 0.321501209705818, 0.950316781444117, + 0.468155469471242, 0.177787119216138, 0.973539858860926, 0.248355853749125, 0.241564859566025 ], [ - 0.0, 0.014856781052379, 0.129593869292673, 0.260893248704942, 0.423991912243378, - 0.535049868821387, 0.673100236377226, 0.773651662250322, 0.842742246177471, 0.856191832671892 + 0.0, 0.921708144384436, 0.322816928053612, 0.319068156961615, 0.950381034454222, + 0.471493235105835, 0.16861229070683, 0.975430067230684, 0.291113822837825, 0.230569184429451 ], ], [ [ - 1.0, 0.984982368516037, 0.904776235744696, 0.768838345408418, 0.601366103261722, - 0.425468373332111, 0.393095121497554, 0.308536491271195, 0.28176339847072, 0.310108549604656 + 1.0, 0.0228111287328508, 0.924686353128632, 0.214481834393956, 0.752637761253111, + 0.230695130910659, 0.760993688283154, 0.2272259018221, 0.652018768425055, 0.489166042327772 ], [ - 1.0, 0.956306348672429, 0.909962584598226, 0.725924615516766, 0.544060805574615, - 0.480362585555218, 0.324556115827164, 0.285575313652313, 0.248117969862161, 0.317230417806702 + 1.0, 0.0255703401212273, 0.864814182600301, 0.192373551594245, 0.758823200454885, + 0.232666864931189, 0.623413212448459, 0.3177990384884, 0.65456289338649, 0.67096938187304 ], [ - 0.0, 0.0540699502487857, 0.127427895274107, 0.227388200675044, 0.405409057344305, - 0.585760550695338, 0.587087195857171, 0.690335032723416, 0.72062886072454, 0.672237389322902 + 0.0, 0.957989755068525, 0.0971494025149874, 0.774170713102742, 0.229738282483638, + 0.814251493721596, 0.267820258695289, 0.742868875733177, 0.316669900469277, 0.52036913803384 ], [ - 0.0, 0.0426463062672075, 0.0886148248885225, 0.271633301034608, 0.456250593646824, - 0.513655628542281, 0.660848353350902, 0.703716116277345, 0.742841532742006, 0.656537894138263 + 0.0, 0.963590414241234, 0.082351514486428, 0.830028986016775, 0.263484269576689, + 0.740360614987001, 0.375542367840523, 0.663439125948471, 0.348352951512707, 0.331769009357169 ], [ - 1.0, 0.963617888001119, 0.921904595006177, 0.719548866059064, 0.626099001065137, - 0.489924131264345, 0.356000117080747, 0.363928457541299, 0.352374557444978, 0.291144894550738 + 1.0, 0.0851374473220236, 0.860266255429572, 0.176631676410624, 0.761001802957984, + 0.307520822631783, 0.705916464541595, 0.488768758146357, 0.412973070839272, 0.56612883649382 ], [ - 1.0, 0.93817598415306, 0.921493568066807, 0.670981991714231, 0.577351413362303, - 0.536934188996764, 0.282142990219803, 0.342630068515299, 0.307918222456741, 0.297193489395872 + 1.0, 0.100008894490174, 0.774976865644717, 0.197583802189653, 0.740876913896688, + 0.380342480510292, 0.506694499272428, 0.638814739397139, 0.380654957457132, 0.662927890888343 ], [ - 0.0, 0.0369014095486213, 0.0803499706244474, 0.282408473275481, 0.372448107305242, - 0.51649774171915, 0.657469866356986, 0.64602226948498, 0.657251128104008, 0.735483313527125 + 0.0, 0.922860601386159, 0.183054750557688, 0.770435004441506, 0.242392082251396, + 0.688021068570413, 0.312162925789055, 0.51694092733334, 0.599504987211551, 0.473081062288769 ], [ - 0.0, 0.0232997445879744, 0.0454703257822588, 0.333276206356045, 0.417014918464293, - 0.451396799888122, 0.738800239854458, 0.659256250605438, 0.689104330186141, 0.720064051637323 + 0.0, 0.922031418597399, 0.212700675709885, 0.844294431992213, 0.251045687086618, + 0.606141523813099, 0.44745658316533, 0.404142633012452, 0.635262470684819, 0.28558863887456 ], ], [ [ - 1.0, 0.934441142438577, 0.904633387248832, 0.771616074990311, 0.65150184829876, - 0.726571696041962, 0.822960080881721, 0.914579693492775, 0.988837315482754, 0.913141022533069 + 1.0, 0.0673429568212604, 0.767116699247112, 0.500736824399411, 0.200310698705978, + 0.897920888884322, 0.0342847808517807, 0.883540050455567, 0.391755755039865, 0.255032830627902 ], [ - 0.0, 0.0850708959538377, 0.11228951582745, 0.22756357540158, 0.353014222711221, - 0.282328684827064, 0.171995662882329, 0.091751390614568, 0.0157218065860296, 0.0862719875351632 + 0.0, 0.928682308414913, 0.258076738445413, 0.488062642285548, 0.804342638068513, + 0.0956185967209478, 0.957909322818776, 0.134869228799696, 0.610031199136574, 0.72419216266817 ], [ - 1.0, 0.929496885176789, 0.868350583884063, 0.801695610308657, 0.688799635770799, - 0.643204934304475, 0.867311376188603, 0.892178999671106, 0.979119123481089, 0.865116938870295 + 1.0, 0.0616327300723214, 0.811913692677124, 0.602441345478833, 0.121334425575055, + 0.964522705233559, 0.0174222244183482, 0.91362546580915, 0.335721369567122, 0.21299405005044 ], [ - 0.0, 0.0511898735601057, 0.114897364301632, 0.198904298071296, 0.306743695898219, - 0.347950445890841, 0.137340860170163, 0.101373247828695, 0.0163518288649865, 0.135074447016374 + 0.0, 0.942116816864899, 0.163686717416269, 0.408465295621045, 0.872300565170837, + 0.0428460852792731, 0.987196284123347, 0.0671209769076069, 0.660849305316163, 0.81026707902818 ], [ - 1.0, 0.935869136131333, 0.902567817119091, 0.770059322032401, 0.653306172909447, - 0.724365061140165, 0.820391964098783, 0.9149394184252, 0.988588770104514, 0.911903885666467 + 1.0, 0.0639765899820351, 0.777824656915726, 0.487621501926283, 0.220361195427173, + 0.891932622576412, 0.0425825576025024, 0.892723839820707, 0.335393387132259, 0.323827553229165 ], [ - 0.0, 0.0833705315516252, 0.114329139990476, 0.229140170930445, 0.350945702842855, - 0.283785515466185, 0.173690888825448, 0.0910715332060352, 0.015769790288882, 0.0868729191060732 + 0.0, 0.930280732926789, 0.245868230438746, 0.509497446736207, 0.777895952299626, + 0.101433323539008, 0.955011697924623, 0.144865869515174, 0.665717444620434, 0.681544055216702 ], [ - 1.0, 0.928391585304243, 0.870172404915299, 0.804286516784946, 0.689242962117821, - 0.646584122130044, 0.871703922427283, 0.893257241469646, 0.980515771019656, 0.864438621941455 + 1.0, 0.0687511134189918, 0.801604570574946, 0.621901676023065, 0.102869728586607, + 0.977798306497945, 0.0248446707924268, 0.88434916690796, 0.383294950282138, 0.147698518890636 ], [ - 0.0, 0.0521699498969902, 0.112759786734776, 0.196734431460449, 0.306445759412875, - 0.345209540197323, 0.13460524452026, 0.100848475268061, 0.0150955942176098, 0.137180177380373 + 0.0, 0.937216751447787, 0.17390869425861, 0.38127326750913, 0.900584796267736, + 0.0279274712600607, 0.980748461393444, 0.0789054019590485, 0.617236588669974, 0.844443750352641 ], ], ] diff --git a/tests/python/test_example_state_to_state.py b/tests/python/test_example_state_to_state.py index 1485e252..b8d5c733 100644 --- a/tests/python/test_example_state_to_state.py +++ b/tests/python/test_example_state_to_state.py @@ -8,27 +8,27 @@ pytestmark = pytest.mark.regression EXPECTED_LENGTH = 1652 -EXPECTED_INFIDELITY = 9.002940985003427e-05 +EXPECTED_INFIDELITY = 8.695620910992297e-06 EXPECTED_PT = [ [ - -0.430435057899144, 0.518899252233085, 0.0553961310496238, -2.73778883011829, 0.836879517793378, - 1.68236101443972, 1.2272994947870701, -1.13889765832405, 2.7441415074662903, 1.68020127607366 + -0.351017944518694, -0.9111853674706331, 1.56337057296288, 1.32102312178454, -2.17680392763229, + -1.91465552269025, -2.40030147272628, 2.44449966044797, 2.09663827839874, -1.0145362400273399 ], ] EXPECTED_QT = [ [ - -1.21262883183835, -2.77343358982429, -2.7674896542845198, -2.81712074264336, -2.71698129305364, - -2.7908352239901797, -2.65458260105392, -2.57461802357684, -2.0247101898358, -1.26152275887394 + -0.35489683713910297, -2.8226401868623903, -2.64838539616493, -1.78300097541017, -2.75117332619166, + -2.7605889552578198, -2.5975353420470504, -2.74733785636068, -2.76505669888879, -2.09765274564999 ], ] EXPECTED_ENERGY = [ [ [ - 0.0, 0.00694252103065461, 0.0331419031652123, 0.0744089523543541, 0.137405174039168, - 0.205169499219536, 0.282367422349298, 0.368813810556003, 0.455572996577133, 0.496383344017663 + 0.0, 0.00692816781265531, 0.0328301908913508, 0.0695204206507133, 0.115813024750246, + 0.172087630256885, 0.251613716534252, 0.336745657239306, 0.426441526468596, 0.502139791825633 ], ], ] @@ -36,8 +36,8 @@ EXPECTED_POPULATION = [ [ [ - 1.0, 0.99305963258305, 0.966873105642978, 0.925633731947042, 0.862637301677907, - 0.794942546965401, 0.717715083633589, 0.631227300284134, 0.54475917511335, 0.503663559455785 + 1.0, 0.993078206283365, 0.967173742450001, 0.930495435459176, 0.884259384596979, + 0.828002837889172, 0.748574947700958, 0.663432310188568, 0.573644729851791, 0.497863551562967 ], ], ] diff --git a/tests/python/test_example_swap02.py b/tests/python/test_example_swap02.py index c0446b56..4ba96fe3 100644 --- a/tests/python/test_example_swap02.py +++ b/tests/python/test_example_swap02.py @@ -7,35 +7,35 @@ pytestmark = pytest.mark.regression EXPECTED_LENGTH = 9906 -EXPECTED_INFIDELITY = 4.30051844739765e-05 +EXPECTED_INFIDELITY = 7.743371585999803e-05 EXPECTED_PT = [ [ - 1.5223974313145099, 0.192367631943629, 5.2322350273611695, 0.270716585386651, 4.38454614717518, - 1.64522208663637, 4.27356481827349, 3.56918803082488, 0.809746914647563, 2.49847938610855 + 3.49878461498772, 0.9372034359063061, 3.82522391628158, 4.94417898650528, 1.6970695838818899, + 3.4150750131677996, 1.64742105679164, 6.27372291394662, -0.158178972429773, 3.33064952087047 ], ] EXPECTED_QT = [ [ - 1.1486887461253, 3.65174025044863, 3.38068715198705, 1.69637577485486, 3.89987855203689, - 1.02123892969852, 4.5603555728564, -0.6527678876090169, 4.1927845343931, 0.519653744346407 + -0.737198940278714, -2.5404099566412097, 1.9430855372276599, -3.29007822559714, 0.202359122117534, + -4.06151032304104, -0.727084835198226, -0.338926104324614, -4.19020409943546, 2.14870117067776 ], ] EXPECTED_ENERGY = [ [ [ - 0.0, 0.0234550545874589, 0.151322202170957, 0.38091326425715, 0.719219922127726, - 1.07914866155792, 1.38417447451341, 1.73449380418287, 1.90878994427285, 2.00000694611498 + 0.0, 0.0617825490723183, 0.176079313745057, 0.439566533429592, 0.717418204516129, + 1.01689731884274, 1.33955354416972, 1.62218904519213, 1.91000947440407, 2.00001741934672 ], [ - 1.0, 1.03449485473264, 1.15972091768289, 1.18383141548819, 1.21165277321355, - 1.20900227532045, 1.21269089383777, 1.11510843776812, 1.0552883588209, 1.00005015890853 + 1.0, 1.02614035923091, 1.13754267526885, 1.24603141974462, 1.2695443541876, + 1.29270091463921, 1.26762689553064, 1.21374312361065, 1.06169311172988, 1.00012844594568 ], [ - 2.0, 1.94250327364116, 1.68998722508855, 1.43543971159315, 1.070068198256, - 0.711977722635159, 0.404183040483815, 0.151128785805321, 0.0373575963912251, 8.02625600351541e-05 + 2.0, 1.91263442680844, 1.68709072718142, 1.31556532558582, 1.01316292157842, + 0.691604243676856, 0.39326419761164, 0.166193685451392, 0.0295179209802044, 0.000174705079131304 ], ], ] @@ -43,16 +43,16 @@ EXPECTED_POPULATION = [ [ [ - 1.0, 0.97686084850513, 0.859760974702525, 0.661516606458905, 0.413421449887264, - 0.205924265997096, 0.0705988862580945, 0.01147031596463, 0.00057333624469326, 3.75770574331089e-06 + 1.0, 0.939711012272369, 0.839442071980425, 0.638260402817787, 0.447629939443993, + 0.235560324657557, 0.0934912404073985, 0.0185680932614118, 0.000551089845975009, 2.31447279607491e-05 ], [ - 0.0, 0.0226342148878521, 0.127263830043423, 0.268727310512903, 0.379204026955525, - 0.377295442001178, 0.277750731738844, 0.13081630109352, 0.0355226617637922, 1.27899738623382e-05 + 0.0, 0.059062097817141, 0.148421106108336, 0.286722174550912, 0.352246338671119, + 0.340677230088839, 0.256925388814789, 0.133855761134253, 0.0279850195965806, 2.43268076119764e-06 ], [ - 0.0, 0.000504783498382912, 0.0129728034877987, 0.0697471926129869, 0.207353891744709, - 0.41674941229836, 0.651616413277044, 0.857685579183397, 0.96388427223235, 0.9999635620352 + 0.0, 0.00122660793399808, 0.0121354622064046, 0.0750116963555328, 0.200113854353825, + 0.423748503355938, 0.649569571317751, 0.847561660359807, 0.971435980115172, 0.999932452759026 ], ], ] diff --git a/tests/python/test_example_swap12.py b/tests/python/test_example_swap12.py index dc050df4..42976b9a 100644 --- a/tests/python/test_example_swap12.py +++ b/tests/python/test_example_swap12.py @@ -8,65 +8,65 @@ pytestmark = pytest.mark.regression EXPECTED_LENGTH = 2770 -EXPECTED_INFIDELITY = 7.825770499303442e-05 +EXPECTED_INFIDELITY = 9.67097896760194e-05 EXPECTED_PT = [ [ - 1.8184215029697302, 6.53541488985085, -12.8815729540948, 13.377779957533601, 15.2559573747476, - -8.55501991773305, -11.571287232539401, 14.5042812158254, -15.2528144059597, -3.12330522923919 + 0.599034888469003, -10.968081982073999, 10.14236927141, 0.496271971161855, -14.8514105719963, + -11.3322163850956, 16.9432995218654, -6.05304529127491, 12.0226355784784, -0.641103780993672 ], [ - -2.87395494164515, 16.170015973460497, 11.9340520593027, 12.4971142034791, -13.5854201479712, - 7.59926941274165, -4.00248319501313, -12.8298360004956, -6.616155043728059, 1.7176979043401601 + 2.48936017685105, -11.2829788700018, -15.755782714853801, -1.69673183942007, 10.9397063530761, + -18.4617758434191, 18.1364064558067, 11.902151691561299, 1.80322805292038, -0.763753212634492 ], ] EXPECTED_QT = [ [ - 3.01935945129119, 16.9528419819601, -4.97870306003347, -6.96804772378882, -0.690015284496415, - 18.0510636076456, -15.4461172849659, 2.94473029913097, -4.26291478812709, 5.15980230386097 + -1.3752677251873802, -5.5464862618018, 3.02923318776624, 13.7730836966518, -1.7585200127837, + -9.011639916323329, -1.02548250949667, -17.9437297458633, -8.87260196321638, -2.93635959571215 ], [ - -6.72607543122026, 8.02402525815179, -14.674541268055801, -12.9299891301557, 12.4372973005706, - 10.031606552642499, -17.2180727670311, -10.4295835579693, -4.794657547857811, 6.95039468450853 + 3.4729999145959702, -9.27721643120156, 4.4305118894594395, 9.31615718399924, 5.30459674901088, + -9.22190274347588, 2.59404705348415, -9.65359045655237, -11.0388548375149, -5.45347255997612 ], ] EXPECTED_ENERGY = [ [ [ - 0.0, 0.47592735741237, 0.920916877121964, 0.806725800976249, 0.655347266057245, - 0.40956010822443, 0.523495640405955, 0.389958416870123, 0.894309878756772, 2.78351042400667e-05 + 0.0, 0.742167645308173, 0.845876123438329, 0.876822609250943, 0.796182302264054, + 0.887353619145017, 0.260229247427617, 0.521386717096158, 0.561593586081704, 3.12615266865929e-05 ], [ - 0.0, 0.506436069839158, 0.894041487982348, 0.482825459874139, 0.263276647075983, - 0.706421192747589, 0.326892966495981, 0.546600128673876, 0.13730502998382, 0.999928014745447 + 0.0, 0.747964174490869, 0.81308548430137, 0.844293448814631, 0.487202832021772, + 0.537027480466349, 0.682308807793044, 0.462529297492794, 0.424555046737099, 0.99992128057069 ], [ - 1.0, 0.517968156465863, 0.0990805037166172, 0.323988697368065, 0.517432001627619, - 0.162284449342756, 0.321486318542869, 0.492744199081024, 0.806922263339182, 7.61332704578777e-05 + 1.0, 0.297596002648272, 0.137778784261989, 0.202672034405316, 0.147534884119167, + 0.382971375153893, 0.651028961021465, 0.290564541017844, 0.466176092802048, 7.51845953833445e-05 ], [ - 1.0, 0.499668416053135, 0.0859611313258037, 0.386460041957824, 0.563944085315957, - 0.721734249708354, 0.82812507503546, 0.570697256338126, 0.161462827349231, 0.999968016951936 + 1.0, 0.212272177599911, 0.203259607969132, 0.0762119075973001, 0.569079981693835, + 0.192647525133809, 0.406432983229683, 0.725519445925169, 0.547675275958967, 0.99997227346397 ], ], [ [ - 0.0, 0.746465361502474, 0.446030321847237, 0.751926392244149, 0.438374046543845, - 0.74788628530837, 0.436255115167154, 0.770339060727562, 0.624611223052237, 2.6314964006988e-05 + 0.0, 0.637458571928054, 0.823214144624233, 0.872362860193146, 0.679904496367965, + 0.653572678063292, 0.256647736263631, 0.111186998901804, 0.0825959025957188, 1.50545826445065e-05 ], [ - 1.0, 0.248921057424824, 0.602804594052578, 0.156112931294798, 0.592388881415718, - 0.565265558947553, 0.492594960169373, 0.912502253042697, 0.677377691676097, 7.8641614022487e-05 + 1.0, 0.366269959795093, 0.166735351026335, 0.219583342381534, 0.193630403614705, + 0.549842705887935, 0.450534076598297, 0.227640166947384, 0.148479698500191, 0.000115585877250997 ], [ - 0.0, 0.69109059043582, 0.612946285346014, 0.774327542815432, 0.102443880083346, - 0.188841270175233, 0.716258624483362, 0.282332897067584, 0.397788656312723, 0.999929501726309 + 0.0, 0.758135709540351, 0.866661271296851, 0.880683596677818, 0.601257953396355, + 0.716614252087069, 0.455517321681113, 0.734916740037334, 0.894595345819472, 0.99993700568465 ], [ - 1.0, 0.313522990987136, 0.338218798604214, 0.317633134502716, 0.866793191462009, - 0.498006885137639, 0.354891300964239, 0.0348257898399821, 0.300222429802531, 0.999965541769708 + 1.0, 0.238135758957608, 0.143389232990416, 0.0273702009562704, 0.525207146619372, + 0.0799703639870128, 0.837300865403608, 0.926256094308564, 0.87432905329638, 0.999932354015059 ], ], ] @@ -74,38 +74,38 @@ EXPECTED_POPULATION = [ [ [ - 1.0, 0.524072642589295, 0.0790831228826006, 0.193274199031723, 0.344652733956227, - 0.590439891793979, 0.476504359618558, 0.610041583159233, 0.105690121275478, 0.999972164930443 + 1.0, 0.257832354742856, 0.154123876614857, 0.123177390836603, 0.203817697828531, + 0.112646380949646, 0.739770752668589, 0.478613283001325, 0.438406414023916, 0.999968738581146 ], [ - 1.0, 0.493563930162257, 0.105958512020345, 0.517174540132416, 0.736723352935244, - 0.2935788072667, 0.67310703352522, 0.45339987135301, 0.862694970044658, 7.1985285512568e-05 + 1.0, 0.252035825513849, 0.186914515704484, 0.155706551197306, 0.512797167991688, + 0.462972519549354, 0.317691192226717, 0.537470702530461, 0.575444953291613, 7.87194605389381e-05 ], [ - 0.0, 0.482031843535495, 0.900919496286575, 0.676011302636772, 0.48256799838042, - 0.837715550669844, 0.678513681476111, 0.507255800952662, 0.193077736696033, 0.999923866768033 + 0.0, 0.702403997357403, 0.862221215745015, 0.797327965602158, 0.852465115890104, + 0.617028624857648, 0.348971038993238, 0.709435459000186, 0.533823907230384, 0.999924815438965 ], [ - 0.0, 0.500331583949654, 0.914038868680122, 0.613539958052361, 0.436055914699638, - 0.278265750311728, 0.171874924988557, 0.42930274369637, 0.838537172687105, 3.198308739064e-05 + 0.0, 0.787727822455274, 0.796740392088216, 0.923788092476431, 0.430920018382434, + 0.807352474945377, 0.593567016851729, 0.274480554157485, 0.452324724162786, 2.77266603992627e-05 ], ], [ [ - 1.0, 0.253534638499192, 0.553969678157327, 0.248073607763823, 0.561625953469628, - 0.25211371471004, 0.563744884857359, 0.229660939301793, 0.375388776980013, 0.999973685070676 + 1.0, 0.362541428122975, 0.176785855428953, 0.1276371398944, 0.320095503724619, + 0.346427322031371, 0.743352263832575, 0.888813001195679, 0.917404097509901, 0.999984945525188 ], [ - 0.0, 0.751078942576592, 0.397195405950115, 0.843887068711757, 0.407611118595508, - 0.434734441066736, 0.507405039851828, 0.0874977469841897, 0.322622308352381, 0.999921358416937 + 0.0, 0.633730040209626, 0.833264648979519, 0.780416657630403, 0.806369596398755, + 0.450157294127768, 0.549465923421463, 0.772359833075872, 0.851520301528521, 0.999884414153978 ], [ - 1.0, 0.308909409565538, 0.387053714657178, 0.225672457189406, 0.897556119924694, - 0.811158729837367, 0.283741375535618, 0.717667102966101, 0.602211343722492, 7.0498312181619e-05 + 1.0, 0.241864290465325, 0.133338728710153, 0.119316403329656, 0.398742046612916, + 0.283385747924472, 0.544482678333589, 0.265083259980697, 0.105404654212961, 6.29943496979323e-05 ], [ - 0.0, 0.686477009015653, 0.661781201401711, 0.682366865507469, 0.133206808553586, - 0.501993114882443, 0.645108699059778, 0.965174210194513, 0.699777570233805, 3.44582696187683e-05 + 0.0, 0.761864241097577, 0.856610767066932, 0.972629799117461, 0.474792853456897, + 0.920029636092174, 0.162699134677804, 0.0737439057740906, 0.125670946825373, 6.76461093106855e-05 ], ], ] From ef6cab53e858c56af02828881356b9d999d50a6f Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 16:36:20 -0600 Subject: [PATCH 28/31] Add tmp directory to evalControls test --- tests/python/test_evalControls.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/python/test_evalControls.py b/tests/python/test_evalControls.py index 8cb90733..a4ea3a5d 100644 --- a/tests/python/test_evalControls.py +++ b/tests/python/test_evalControls.py @@ -1,12 +1,14 @@ import numpy as np +import os from pytest import approx from quandary import Quandary -def test_evalControls_updates_timestep(): +def test_evalControls_updates_timestep(tmp_path, request): """ Test that evalControls properly updates timestep (dT) to match sampling rate. """ + datadir_path = os.path.join(tmp_path, request.node.name) T = 5.0 quandary = Quandary( @@ -21,7 +23,7 @@ def test_evalControls_updates_timestep(): # Test evalControls with different sampling rate points_per_ns = 2 - time, _, _ = quandary.evalControls(points_per_ns=points_per_ns) + time, _, _ = quandary.evalControls(points_per_ns=points_per_ns, datadir=datadir_path) expected_nsteps = int(np.floor(T * points_per_ns)) expected_dT = T / expected_nsteps From bff294340b033bea8e58a8a65b2f2ecfc149bc79 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 17:56:49 -0600 Subject: [PATCH 29/31] Use mpiexec fixture --- tests/python/test_evalControls.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/python/test_evalControls.py b/tests/python/test_evalControls.py index a4ea3a5d..f5db4018 100644 --- a/tests/python/test_evalControls.py +++ b/tests/python/test_evalControls.py @@ -4,7 +4,7 @@ from quandary import Quandary -def test_evalControls_updates_timestep(tmp_path, request): +def test_evalControls_updates_timestep(mpi_exec, tmp_path, request): """ Test that evalControls properly updates timestep (dT) to match sampling rate. """ @@ -23,7 +23,7 @@ def test_evalControls_updates_timestep(tmp_path, request): # Test evalControls with different sampling rate points_per_ns = 2 - time, _, _ = quandary.evalControls(points_per_ns=points_per_ns, datadir=datadir_path) + time, _, _ = quandary.evalControls(points_per_ns=points_per_ns, datadir=datadir_path, mpi_exec=mpi_exec) expected_nsteps = int(np.floor(T * points_per_ns)) expected_dT = T / expected_nsteps From 51e3ddb441dae58f9d51732baf077621021cabcf Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 1 Jul 2025 17:57:16 -0600 Subject: [PATCH 30/31] Always using mpiexec, even for single process, as on slurm this is needed --- quandary.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/quandary.py b/quandary.py index 4694ce38..4c5f45b4 100644 --- a/quandary.py +++ b/quandary.py @@ -1426,12 +1426,11 @@ def execute(*, runtype="simulation", ncores=1, config_filename="config.cfg", dat if not verbose: runcommand += " --quiet" # If parallel run, specify runcommand. Default is 'mpirun -np ', unless batch args are given, then currently using 'srun -n', see end of this file for changing that to other batch systems. - if ncores > 1: - if len(batchargs)>0: - myrun = batch_run # currently set to "srun -n" - else: - myrun = mpi_exec - runcommand = f"{myrun} {ncores} " + runcommand + if len(batchargs)>0: + myrun = batch_run # currently set to "srun -n" + else: + myrun = mpi_exec + runcommand = f"{myrun} {ncores} " + runcommand if verbose: print("Running Quandary ... ") From 37895ac9cf085072db159ae63c952f090458e10a Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Wed, 2 Jul 2025 08:41:51 -0600 Subject: [PATCH 31/31] Allow for passing mpi options to the python tests --- tests/python/conftest.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/python/conftest.py b/tests/python/conftest.py index 6c818bc5..ee6c79dc 100644 --- a/tests/python/conftest.py +++ b/tests/python/conftest.py @@ -5,8 +5,9 @@ @pytest.fixture def mpi_exec(request): - """Get MPI executor from pytest option.""" + """Get MPI executor and options from pytest options.""" executor = request.config.getoption("--mpi-exec") + options = request.config.getoption("--mpi-opt") if executor != "mpirun": - return f"{executor} -n " - return "mpirun -np " + return f"{executor} {options} -n" + return f"mpirun {options} -np"