Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions tests/python/test_example_cnot_const_init_ctrl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import os
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 = 3.6979557855976175e-05

EXPECTED_PT = [
[
69.4004584740497, -56.4748587229514, 14.6194226065187, 41.3022123079845, -67.73628866574009,
62.065452347372606, -38.9624100460397, -12.200805256966099, 55.4407922797055, -70.0787542955629
],
[
73.229765747718, -51.530356030475104, 10.797743240407, 32.9999831592717, -65.9215400319986,
68.1157477500591, -36.665422937239306, -22.0688444356676, 53.2241318694266, -70.762850942261
],
]

EXPECTED_QT = [
[
69.6429908212518, -56.1952283280093, 17.2100586110721, 40.5719025432599, -67.2782353436477,
62.8808620909873, -35.1388165423998, -12.2276107374885, 56.0566382776629, -69.8661090578335
],
[
74.3860436073452, -49.769725517584696, 14.8400227183943, 35.952563400735, -66.7343819618446,
67.61236549795551, -33.3579362154448, -15.5147165351889, 55.498385570977604, -68.6915883259813
],
]

EXPECTED_ENERGY = [
[
[
0.0, 0.146434155154378, 0.527396222930252, 0.8333688467604, 0.178103890140801,
0.520428421328945, 0.341607833241572, 0.430496355266566, 0.414851527192667, 6.96474616175235e-06
],
[
0.0, 0.299436918554029, 0.623281309650759, 0.727281777811903, 0.29392649895835,
0.460459280349417, 0.338491303457183, 0.148098740071313, 0.542120540677145, 6.90391354963638e-06
],
[
1.0, 0.912189864480567, 0.489755781185604, 0.120883431289255, 0.704407057315191,
0.362090662066901, 0.708332294696574, 0.819241990240916, 0.446192955946767, 0.99999708945145
],
[
1.0, 0.641939061924855, 0.359566686519967, 0.318465944618273, 0.823562554277443,
0.657021637296419, 0.611568569732679, 0.602162915288405, 0.596834977772345, 0.999989043578022
],
],
[
[
0.0, 0.849674867882648, 0.311347334072333, 0.920830873299062, 0.267622719176619,
0.264811363494957, 0.684475235376846, 0.8088259589714, 0.725432021094394, 7.02591458519224e-06
],
[
1.0, 0.181716864915869, 0.59806074182981, 0.205466706171419, 0.700728634090143,
0.65181512506587, 0.236588536813724, 0.124726329034347, 0.282435940220941, 0.99998895407904
],
[
0.0, 0.714160901457531, 0.146494043882507, 0.475422969060653, 0.538326634571238,
0.878584814887598, 0.248620647645974, 0.189155370811277, 0.412335586770598, 0.999986055714482
],
[
1.0, 0.254447366037388, 0.944097880652566, 0.398279452064191, 0.493322012669281,
0.204788697406206, 0.83031558115938, 0.877292342095324, 0.579796452768217, 1.79659758716049e-05
],
],
]

EXPECTED_POPULATION = [
[
[
1.0, 0.85356584488177, 0.472603777164144, 0.166631153403343, 0.821896110096733,
0.479571579021539, 0.658392167182505, 0.569503645283894, 0.585148473450691, 0.999993035982757
],
[
1.0, 0.700563081508297, 0.376718690549668, 0.272718222510303, 0.706073501460846,
0.539540720136406, 0.66150869709237, 0.851901260630598, 0.457879460111683, 0.999993096979301
],
[
0.0, 0.0878101355829006, 0.51024421898866, 0.879116569013979, 0.295592943063672,
0.63790933838759, 0.291667705819821, 0.180758010409327, 0.553807044816912, 2.91134124264776e-06
],
[
0.0, 0.358060938162397, 0.640433313690709, 0.681534055688347, 0.176437446110549,
0.342978363166151, 0.388431430809871, 0.397837085391043, 0.403165023007325, 1.0957318515284e-05
],
],
[
[
1.0, 0.150325132153501, 0.688652666022062, 0.0791691268646811, 0.732377281060915,
0.735188636855528, 0.31552476504723, 0.19117404157906, 0.274567979548965, 0.999992974814333
],
[
0.0, 0.818283135146458, 0.401939258370617, 0.794533294150787, 0.299271366329053,
0.348184875419952, 0.763411463735828, 0.875273671667564, 0.717564060567887, 1.10468138109779e-05
],
[
1.0, 0.285839098605937, 0.853505956291756, 0.52457703124258, 0.461673365807625,
0.121415185566892, 0.75137935287042, 0.810844629838966, 0.587664413993081, 1.39450782101164e-05
],
[
0.0, 0.745552634049864, 0.0559021195581093, 0.601720548242429, 0.506677987718712,
0.795211303056364, 0.16968441938317, 0.122707658584124, 0.420203548011452, 0.999982034920666
],
],
]

# 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_const_init_ctrl(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)
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
n_osc = 2
n_levels = 4

quandary = Quandary(
freq01=freq01,
Jkl=[0.005],
rotfreq=rotfreq,
T=T,
targetgate=unitary,
verbose=False,
initctrl_MHz=100.0,
randomize_init_ctrl=False,
)

t, pt, qt, infidelity, energy, population = quandary.optimize(
mpi_exec=mpi_exec,
maxcores=2,
datadir=datadir_path,
)

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
)
Loading