|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +import numpy as np |
| 4 | +from quandary import Quandary |
| 5 | +from utils import assert_results_equal, print_expected_values |
| 6 | + |
| 7 | +# Mark all tests in this file as regression tests |
| 8 | +pytestmark = pytest.mark.regression |
| 9 | + |
| 10 | +EXPECTED_LENGTH = 1222 |
| 11 | +EXPECTED_INFIDELITY = 3.6979557855976175e-05 |
| 12 | + |
| 13 | +EXPECTED_PT = [ |
| 14 | + [ |
| 15 | + 69.4004584740497, -56.4748587229514, 14.6194226065187, 41.3022123079845, -67.73628866574009, |
| 16 | + 62.065452347372606, -38.9624100460397, -12.200805256966099, 55.4407922797055, -70.0787542955629 |
| 17 | + ], |
| 18 | + [ |
| 19 | + 73.229765747718, -51.530356030475104, 10.797743240407, 32.9999831592717, -65.9215400319986, |
| 20 | + 68.1157477500591, -36.665422937239306, -22.0688444356676, 53.2241318694266, -70.762850942261 |
| 21 | + ], |
| 22 | +] |
| 23 | + |
| 24 | +EXPECTED_QT = [ |
| 25 | + [ |
| 26 | + 69.6429908212518, -56.1952283280093, 17.2100586110721, 40.5719025432599, -67.2782353436477, |
| 27 | + 62.8808620909873, -35.1388165423998, -12.2276107374885, 56.0566382776629, -69.8661090578335 |
| 28 | + ], |
| 29 | + [ |
| 30 | + 74.3860436073452, -49.769725517584696, 14.8400227183943, 35.952563400735, -66.7343819618446, |
| 31 | + 67.61236549795551, -33.3579362154448, -15.5147165351889, 55.498385570977604, -68.6915883259813 |
| 32 | + ], |
| 33 | +] |
| 34 | + |
| 35 | +EXPECTED_ENERGY = [ |
| 36 | + [ |
| 37 | + [ |
| 38 | + 0.0, 0.146434155154378, 0.527396222930252, 0.8333688467604, 0.178103890140801, |
| 39 | + 0.520428421328945, 0.341607833241572, 0.430496355266566, 0.414851527192667, 6.96474616175235e-06 |
| 40 | + ], |
| 41 | + [ |
| 42 | + 0.0, 0.299436918554029, 0.623281309650759, 0.727281777811903, 0.29392649895835, |
| 43 | + 0.460459280349417, 0.338491303457183, 0.148098740071313, 0.542120540677145, 6.90391354963638e-06 |
| 44 | + ], |
| 45 | + [ |
| 46 | + 1.0, 0.912189864480567, 0.489755781185604, 0.120883431289255, 0.704407057315191, |
| 47 | + 0.362090662066901, 0.708332294696574, 0.819241990240916, 0.446192955946767, 0.99999708945145 |
| 48 | + ], |
| 49 | + [ |
| 50 | + 1.0, 0.641939061924855, 0.359566686519967, 0.318465944618273, 0.823562554277443, |
| 51 | + 0.657021637296419, 0.611568569732679, 0.602162915288405, 0.596834977772345, 0.999989043578022 |
| 52 | + ], |
| 53 | + ], |
| 54 | + [ |
| 55 | + [ |
| 56 | + 0.0, 0.849674867882648, 0.311347334072333, 0.920830873299062, 0.267622719176619, |
| 57 | + 0.264811363494957, 0.684475235376846, 0.8088259589714, 0.725432021094394, 7.02591458519224e-06 |
| 58 | + ], |
| 59 | + [ |
| 60 | + 1.0, 0.181716864915869, 0.59806074182981, 0.205466706171419, 0.700728634090143, |
| 61 | + 0.65181512506587, 0.236588536813724, 0.124726329034347, 0.282435940220941, 0.99998895407904 |
| 62 | + ], |
| 63 | + [ |
| 64 | + 0.0, 0.714160901457531, 0.146494043882507, 0.475422969060653, 0.538326634571238, |
| 65 | + 0.878584814887598, 0.248620647645974, 0.189155370811277, 0.412335586770598, 0.999986055714482 |
| 66 | + ], |
| 67 | + [ |
| 68 | + 1.0, 0.254447366037388, 0.944097880652566, 0.398279452064191, 0.493322012669281, |
| 69 | + 0.204788697406206, 0.83031558115938, 0.877292342095324, 0.579796452768217, 1.79659758716049e-05 |
| 70 | + ], |
| 71 | + ], |
| 72 | +] |
| 73 | + |
| 74 | +EXPECTED_POPULATION = [ |
| 75 | + [ |
| 76 | + [ |
| 77 | + 1.0, 0.85356584488177, 0.472603777164144, 0.166631153403343, 0.821896110096733, |
| 78 | + 0.479571579021539, 0.658392167182505, 0.569503645283894, 0.585148473450691, 0.999993035982757 |
| 79 | + ], |
| 80 | + [ |
| 81 | + 1.0, 0.700563081508297, 0.376718690549668, 0.272718222510303, 0.706073501460846, |
| 82 | + 0.539540720136406, 0.66150869709237, 0.851901260630598, 0.457879460111683, 0.999993096979301 |
| 83 | + ], |
| 84 | + [ |
| 85 | + 0.0, 0.0878101355829006, 0.51024421898866, 0.879116569013979, 0.295592943063672, |
| 86 | + 0.63790933838759, 0.291667705819821, 0.180758010409327, 0.553807044816912, 2.91134124264776e-06 |
| 87 | + ], |
| 88 | + [ |
| 89 | + 0.0, 0.358060938162397, 0.640433313690709, 0.681534055688347, 0.176437446110549, |
| 90 | + 0.342978363166151, 0.388431430809871, 0.397837085391043, 0.403165023007325, 1.0957318515284e-05 |
| 91 | + ], |
| 92 | + ], |
| 93 | + [ |
| 94 | + [ |
| 95 | + 1.0, 0.150325132153501, 0.688652666022062, 0.0791691268646811, 0.732377281060915, |
| 96 | + 0.735188636855528, 0.31552476504723, 0.19117404157906, 0.274567979548965, 0.999992974814333 |
| 97 | + ], |
| 98 | + [ |
| 99 | + 0.0, 0.818283135146458, 0.401939258370617, 0.794533294150787, 0.299271366329053, |
| 100 | + 0.348184875419952, 0.763411463735828, 0.875273671667564, 0.717564060567887, 1.10468138109779e-05 |
| 101 | + ], |
| 102 | + [ |
| 103 | + 1.0, 0.285839098605937, 0.853505956291756, 0.52457703124258, 0.461673365807625, |
| 104 | + 0.121415185566892, 0.75137935287042, 0.810844629838966, 0.587664413993081, 1.39450782101164e-05 |
| 105 | + ], |
| 106 | + [ |
| 107 | + 0.0, 0.745552634049864, 0.0559021195581093, 0.601720548242429, 0.506677987718712, |
| 108 | + 0.795211303056364, 0.16968441938317, 0.122707658584124, 0.420203548011452, 0.999982034920666 |
| 109 | + ], |
| 110 | + ], |
| 111 | +] |
| 112 | + |
| 113 | +# Compare output to expected result for 10 points |
| 114 | +NUM_SAMPLES = 10 |
| 115 | +SAMPLE_INDICES = [int(i * (EXPECTED_LENGTH - 1) / (NUM_SAMPLES - 1)) for i in range(NUM_SAMPLES)] |
| 116 | + |
| 117 | + |
| 118 | +def test_example_cnot_const_init_ctrl(mpi_exec, tmp_path, request): |
| 119 | + """Test CNOT gate optimization using Python interface.""" |
| 120 | + datadir_path = os.path.join(tmp_path, request.node.name) |
| 121 | + print(f"datadir is {datadir_path}") |
| 122 | + |
| 123 | + freq01 = [4.80595, 4.8601] |
| 124 | + favg = sum(freq01)/len(freq01) |
| 125 | + rotfreq = favg*np.ones(len(freq01)) |
| 126 | + T = 200.0 |
| 127 | + |
| 128 | + unitary = np.identity(4) |
| 129 | + unitary[2, 2] = 0.0 |
| 130 | + unitary[3, 3] = 0.0 |
| 131 | + unitary[2, 3] = 1.0 |
| 132 | + unitary[3, 2] = 1.0 |
| 133 | + n_osc = 2 |
| 134 | + n_levels = 4 |
| 135 | + |
| 136 | + quandary = Quandary( |
| 137 | + freq01=freq01, |
| 138 | + Jkl=[0.005], |
| 139 | + rotfreq=rotfreq, |
| 140 | + T=T, |
| 141 | + targetgate=unitary, |
| 142 | + verbose=False, |
| 143 | + initctrl_MHz=100.0, |
| 144 | + randomize_init_ctrl=False, |
| 145 | + ) |
| 146 | + |
| 147 | + t, pt, qt, infidelity, energy, population = quandary.optimize( |
| 148 | + mpi_exec=mpi_exec, |
| 149 | + maxcores=2, |
| 150 | + datadir=datadir_path, |
| 151 | + ) |
| 152 | + |
| 153 | + print_expected_values(infidelity, pt, qt, energy, population, SAMPLE_INDICES, n_osc) |
| 154 | + assert_results_equal( |
| 155 | + t=t, |
| 156 | + pt=pt, |
| 157 | + qt=qt, |
| 158 | + infidelity=infidelity, |
| 159 | + energy=energy, |
| 160 | + population=population, |
| 161 | + T=T, |
| 162 | + n_osc=n_osc, |
| 163 | + n_levels=n_levels, |
| 164 | + expected_length=EXPECTED_LENGTH, |
| 165 | + expected_infidelity=EXPECTED_INFIDELITY, |
| 166 | + expected_pt=EXPECTED_PT, |
| 167 | + expected_qt=EXPECTED_QT, |
| 168 | + expected_energy=EXPECTED_ENERGY, |
| 169 | + expected_population=EXPECTED_POPULATION, |
| 170 | + sample_indices=SAMPLE_INDICES |
| 171 | + ) |
0 commit comments