-
Notifications
You must be signed in to change notification settings - Fork 9
/
circuitgeometry.cpp
1795 lines (1564 loc) · 52 KB
/
circuitgeometry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <algorithm>
#include <cmath>
#include "circuitgeometry.h"
#include "cnotcounter.h"
long gridPosition::totalWires = 0;
long gridPosition::logicalQubitsPerLine = 5;
gridPosition::gridPosition()
{
gridRow = 0;
gridColumn = 0;
isControl = false;
isFakeTarget = false;
}
gridPosition::gridPosition(const gridPosition& c)
{
gridRow = c.gridRow;
gridColumn = c.gridColumn;
isControl = c.isControl;
isFakeTarget = c.isFakeTarget;
}
gridPosition& gridPosition::operator= (const gridPosition& c)
{
gridRow = c.gridRow;
gridColumn = c.gridColumn;
isControl = c.isControl;
isFakeTarget = c.isFakeTarget;
return *this;
}
gridPosition circuitgeometry::getWirePositionInGrid(long wire)
{
long totalNrRows = (long) ceil ((gridPosition::totalWires + 0.0) / gridPosition::logicalQubitsPerLine);
gridPosition ret;
/*
* 3 NOV
* the negative wires (e.g. -1) represent the place where the T gate is connected
*/
if(wire >= 0)
{
ret.gridRow = (wire + totalNrRows) % totalNrRows;
ret.gridColumn = (wire + totalNrRows) / totalNrRows - 1;
//make it with curves
if(!ret.isEvenGridColumn())
{
ret.gridRow = (totalNrRows - 1) - ret.gridRow;
}
ret.gridRow *= -1;
}
else
{
ret.gridRow = 1;
ret.gridColumn = -1 - wire;
}
// gridPosition ret;
// ret.gridRow = -((wire + logicalQubitsPerLine) / logicalQubitsPerLine - 1);
// ret.gridColumn = (wire + logicalQubitsPerLine) % logicalQubitsPerLine;
//
// //make it with curves
// // 1 2 3
// // 6 5 4
// // 7 8 9
// if(!ret.isEvenGridRow())
// {
// ret.gridColumn = (logicalQubitsPerLine - 1) - ret.gridColumn;
// }
return ret;
}
bool gridPosition::isEvenGridRow()
{
return abs(gridRow) % 2 != 1;
}
bool gridPosition::isEvenGridColumn()
{
// if(gridRow == 1)
// return false;
return abs(gridColumn) % 2 != 1;
}
int circuitgeometry::getInputCoordAxis()
{
//27.10.2017
// int where = inputConnectionDown ? CIRCUITDEPTH : CIRCUITHEIGHT;
int where = CIRCUITWIDTH;
return where;
}
void circuitgeometry::liAddPinPair(int wire, int ioIndex, int type)
{
pinpair pins;//8 ints with value -100
pins[TYPE] = type;
pins[INJNR] = ioIndex;
pindetails detail1;
detail1.coord = simplegeom.coords[lastIndex[wire][CURR1]];
pindetails detail2;
detail2.coord = simplegeom.coords[lastIndex[wire][CURR2]];
pins.setPinDetail(0, detail1);
pins.setPinDetail(1, detail2);
allpins.addEntry(pins);
}
void circuitgeometry::liConnectPrevsWithCurrs(int wire)
{
//add segments to the left
for (int si = 0; si < 2; si++)
{
simplegeom.addSegment(lastIndex[wire][PREV1 + si], lastIndex[wire][CURR1 + si], DONOTADDEXISTINGSEGMENT);
}
}
void circuitgeometry::liConnectIOPointToCurrs(int wire, int& ioIndex, bool connectionDown)
{
//not used?
if (connectionDown)
{
//add segment down
simplegeom.addSegment(lastIndex[wire][CURR1], lastIndex[wire][CURR2], DONOTADDEXISTINGSEGMENT);
//create a point after the ioIndex
convertcoordinate nioc(simplegeom.coords[ioIndex]);
nioc[CIRCUITDEPTH] += DELTA / 2;
int nindex = simplegeom.addCoordinate(nioc);
//add segment to the right, between curr1 and ioIndex
simplegeom.addSegment(lastIndex[wire][CURR1], ioIndex, DONOTADDEXISTINGSEGMENT);
//add segment between ioIndex and point following it
simplegeom.addSegment(ioIndex, nindex, DONOTADDEXISTINGSEGMENT);
//update CURR1
lastIndex[wire][CURR1] = nindex;
//at this point CURR1 and CURR2 are not parallel anymore
//CURR1 has an offset of 2 on the circuitdepth axis
}
else
{
//add segments between the CURR points and the ioIndex
for (int si = 0; si < 2; si++)
{
simplegeom.addSegment(ioIndex, lastIndex[wire][CURR1 + si], DONOTADDEXISTINGSEGMENT);
}
}
}
int circuitgeometry::liInsertIOPointUsingCurr1(int wire, bool isInit)
{
//the io will be with one layer up
convertcoordinate ioc(simplegeom.coords.at(lastIndex[wire][CURR1]));
//ioc.assign(coords.at(lastindex[CURR1]).begin(), coords.at(lastindex[CURR1]).end());
//29.10.2017
// int where = isInit ? getInputCoordAxis() : CIRCUITHEIGHT;
int where = isInit ? getInputCoordAxis() : CIRCUITWIDTH;
ioc[where] += DELTA/2;
int ioIndex = simplegeom.addIOPoint(ioc, isInit);
return ioIndex;
}
void circuitgeometry::liCopyCurrsOverPrevs(int wire)
{
//move the curr indices to be prev indices
for (int si = 0; si < 2; si++)
{
lastIndex[wire][PREV1 + si] = lastIndex[wire][CURR1 + si]; //will be used at the later point at indices 0,1
}
}
int circuitgeometry::liAddIOPoint2(int wire)
{
convertcoordinate ioc;
for(int i=0; i<3; i++)
{
ioc[i] = (simplegeom.coords[lastIndex[wire][CURR1]][i] + simplegeom.coords[lastIndex[wire][CURR2]][i])/2;
if(ioc[i] != simplegeom.coords[lastIndex[wire][CURR1]][i])
{
//daca se afla la mijlocul distantei intre CURR1 si CURR2
//coordonatele defectelor sunt in unitati de celula
//coordonatele io sunt in qubits
//deci celulele primare au coordonate toate impare
if(ioc[i] % 2 == 1)
{
//daca e coordonata de celula
ioc[i]--;//par e coordonata de qubit
}
}
}
int ioIndex = simplegeom.addIOPoint(ioc, true);
return ioIndex;
}
void circuitgeometry::addHadamard(int& wire, int level)
{
liInitCurrDual(wire, level);
liDualAddAxisOffset(CURR1, CIRCUITWIDTH, -DELTA);
liDualAddAxisOffset(CURR2, CIRCUITWIDTH, -DELTA);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITHEIGHT, -DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, -DELTA);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITWIDTH, 2 * DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, 2 * DELTA);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITHEIGHT, DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, DELTA);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITWIDTH, -2 * DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, -2 * DELTA);
}
void circuitgeometry::addSGate(int& wire, int level)
{
liInitCurrDual(wire, level);
liDualAddAxisOffset(CURR1, CIRCUITWIDTH, -DELTA);
liDualAddAxisOffset(CURR2, CIRCUITWIDTH, -DELTA);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITHEIGHT, -DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, -DELTA);
simplegeom.addSegment(lastDualIndices[CURR1], lastDualIndices[CURR2], DONOTADDEXISTINGSEGMENT);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITWIDTH, 2 * DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, 2 * DELTA);
simplegeom.addSegment(lastDualIndices[CURR1], lastDualIndices[CURR2], DONOTADDEXISTINGSEGMENT);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITHEIGHT, DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, DELTA);
simplegeom.addSegment(lastDualIndices[CURR1], lastDualIndices[CURR2], DONOTADDEXISTINGSEGMENT);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITWIDTH, -2 * DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, -2 * DELTA);
simplegeom.addSegment(lastDualIndices[CURR1], lastDualIndices[CURR2], DONOTADDEXISTINGSEGMENT);
}
void circuitgeometry::addVGate(int& wire, int level)
{
liInitCurrDual(wire, level);
liDualAddAxisOffset(CURR1, CIRCUITWIDTH, -DELTA);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITHEIGHT, -DELTA);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITWIDTH, 2 * DELTA);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITDEPTH, DELTA);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITHEIGHT, DELTA);
liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITWIDTH, - 2 * DELTA);
// liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITDEPTH, -DELTA);
}
void circuitgeometry::makeGeometryFromCircuit(bfsState& state)
{
// TODO: Is this constantly needed?
// TODO: move to an initi method in circuitgeometry
lastIndex.resize(state.getNrLines());
for(size_t i=0; i<lastIndex.size(); i++)
{
lastIndex[i].resize(4);
}
lastDualIndices.resize(4);
lastDualIndicesAxisSegment.resize(4, -1);
size_t pos = 0;
for(std::vector<recyclegate*>::iterator it = state.toDraw.begin();
it != state.toDraw.end(); it++)
{
pos++;
printf("draw %lu(ordnr_%ld id_%ld). %c [", pos, (*it)->orderNrInGateList, (*it)->getId(), (*it)->type);
// if((*it)->type == 'c')
// {
// (*it)->print();
// }
for(size_t i = 0; i < (*it)->wirePointers.size(); i++)
{
printf(" %ld", (*it)->wirePointers[i]->number);
}
printf("] l%ld\n", (*it)->level);
bool isCNOT = (*it)->type == 'c';
bool isTGate = (*it)->type == 't';
bool isHGate = (*it)->type == 'h';
bool isSGate = (*it)->type == 'p';
bool isVGate = (*it)->type == 'v';
bool isInit = (*it)->isInitialisation();
bool isMeasure = (*it)->isMeasurement();
int level = (*it)->level;
int wire = (*it)->wirePointers[0]->number;
//initialise the CURR points
//and update them according to their meaning
if(isCNOT || isInit || isMeasure || isTGate || isHGate)
{
liInitCurrPrimal(wire, level);
}
/*
* Inputs are moved -25 to the left
*/
// if((*it)->isInput())
// {
// liAddAxisOffset(wire, CURR1, CIRCUITDEPTH, -25 * DELTA);
// liAddAxisOffset(wire, CURR2, CIRCUITDEPTH, -25 * DELTA);
// }
if(isCNOT)//27.oct.2017
{
addCnotPrimal(wire);
recyclegate* operationPtr = *it;
std::vector<long> targets;
for(std::vector<wireelement*>::iterator it = operationPtr->wirePointers.begin();
it != operationPtr->wirePointers.end(); it++)
{
targets.push_back((*it)->number);
}
addCnotDual(targets, operationPtr->level, true);
}
/*
* Measurements
*/
if(isMeasure)
{
liAddAxisOffset(wire, CURR1, CIRCUITDEPTH, DELTA);
liAddAxisOffset(wire, CURR2, CIRCUITDEPTH, DELTA);
liConnectPrevsWithCurrs(wire);
//add the io measurement point
int ioIndex = liInsertIOPointUsingCurr1(wire, false);
liConnectIOPointToCurrs(wire, ioIndex, false);
}
/*
* Initialisations
*/
if(isInit)
{
// simplegeom.addSegment(lastIndex[wire][CURR1], lastIndex[wire][CURR2]);
// liAddAxisOffset(wire, CURR2, CIRCUITDEPTH, DELTA);
// liAddAxisOffset(wire, CURR1, CIRCUITHEIGHT, DELTA);
//
/**
* THIS NEEDS TO BE REPAIRED
*/
// int ioIndex = liAddIOPoint2(wire);
//
// if((*it)->type == AA || (*it)->type == YY)
// {
// liAddPinPair(wire, ioIndex, (*it)->type == AA ? ATYPE : YTYPE);
//
// state.operationIdToCircuitPinIndex[(*it)->getId()] = allpins.size() - 1;
// }
// else
// {
// //pentru a marca care sunt injectii, la inputuri leg de io, iar la injectii nu
// simplegeom.addSegment(ioIndex, lastIndex[wire][CURR1]);
// simplegeom.addSegment(ioIndex, lastIndex[wire][CURR2]);
// }
//
// liAddAxisOffset(wire, CURR1, CIRCUITHEIGHT, -DELTA);
//add the io measurement point
int ioIndex = liInsertIOPointUsingCurr1(wire, true);
liConnectIOPointToCurrs(wire, ioIndex, false);
}
if(isTGate)
{
addCnotPrimal(wire);
//
// //the A state pins
gridPosition position = getWirePositionInGrid(-1);
//modificat 29.10.2017 sa fie liniile paralele si nu una sub cealalalta
// convertcoordinate c1;
// c1[CIRCUITWIDTH] = position.gridColumn * 2 * DELTA + 1;
// c1[CIRCUITDEPTH] = level * getDepthShift() + 1; //compacted to bridge?
// c1[CIRCUITHEIGHT] = position.gridRow * DELTA + 1;
/*NOW DUAL COORDS*/
convertcoordinate c1;
c1[CIRCUITWIDTH] = ((*it)->connChannel) * 2 * DELTA + getPrevHalfDelta(1);
c1[CIRCUITDEPTH] = level * getDepthShift() - getPrevHalfDelta(-1); //compacted to bridge?
c1[CIRCUITHEIGHT] = DELTA + getPrevHalfDelta(1);// + ((*it)->connChannel % 2 == 0 ? - DELTA : 0);
convertcoordinate c2 (c1);
// c2[CIRCUITWIDTH] += DELTA;
//the pins are along the depth axis
c2[CIRCUITDEPTH] += DELTA;
//the io will be with one layer up
convertcoordinate ioc(c1);
int where = CIRCUITDEPTH;//isInit ? getInputCoordAxis() : CIRCUITWIDTH;
ioc[where] += DELTA/2;
int ioIndex1 = simplegeom.addIOPoint(ioc, isInit);
pinpair pins;
pins[TYPE] = ATYPE;
pins[INJNR] = ioIndex1;
pindetails detail1;
detail1.coord = c1;
pindetails detail2;
detail2.coord = c2;
pins.setPinDetail(0, detail1);
pins.setPinDetail(1, detail2);
allpins.addEntry(pins);
state.operationIdToCircuitPinIndex[(*it)->getId()] = allpins.size() - 1;
recyclegate* operationPtr = *it;
std::vector<long> targets;
targets.push_back(operationPtr->wirePointers[0]->number);
/*
* 3NOV - neglect the dummy zero wire
*
* from the T gate which was added in circconvert.cpp
*/
targets.push_back(-(*it)->connChannel - 1);
addCnotDual(targets, operationPtr->level, false);
/*
* elimina segmentul care contine pinii drept capete
*/
int idx1 = simplegeom.addCoordinate(c1);
int idx2 = simplegeom.addCoordinate(c2);
simplegeom.removeSegment(idx1, idx2);
}
if(isHGate)
{
addHadamard(wire, level);
}
if(isSGate)
{
addSGate(wire, level);
}
if(isVGate)
{
addVGate(wire, level);
}
//move the curr indices to be prev indices
if(isCNOT || isInit || isMeasure)
{
liCopyCurrsOverPrevs(wire);
}
}
}
void circuitgeometry::addCnotPrimal(int& wire)
{
if (!useBridge)
{
liAddAxisOffset(wire, CURR1, CIRCUITDEPTH, -DELTA);
liAddAxisOffset(wire, CURR2, CIRCUITDEPTH, -DELTA);
}
//add segments to the left
liConnectPrevsWithCurrs(wire);
//first down
simplegeom.addSegment(lastIndex[wire][CURR1], lastIndex[wire][CURR2], DONOTADDEXISTINGSEGMENT);
if (!useBridge)
{
liAddAxisOffset(wire, CURR1, CIRCUITDEPTH, DELTA);
liAddAxisOffset(wire, CURR2, CIRCUITDEPTH, DELTA);
//second down - non-bridge?
simplegeom.addSegment(lastIndex[wire][CURR1], lastIndex[wire][CURR2], DONOTADDEXISTINGSEGMENT);
}
}
std::vector<gridPosition> circuitgeometry::computePositionsForCnotDual(std::vector<long>& targets)
{
std::vector<gridPosition> positionsToDraw;
if(targets.size() == 2 && targets[1] < 0)
{
//place the first element
gridPosition position0 = getWirePositionInGrid(targets[0]);
position0.isControl = true;//the first one is control
gridPosition position1 = getWirePositionInGrid(targets[1]);
positionsToDraw.push_back(position0);
positionsToDraw.push_back(position1);
/**
* This is the situation for the T gate
*/
bool swap = false;
//do not sort because this is a T gate?
if(position1.gridColumn < position0.gridColumn)
{
swap = true;
}
else if(position1.gridColumn == position0.gridColumn)
{
//check column direction
//target1 has always positive row and is higher than the control
if(position1.isEvenGridColumn())
{
swap = true;
}
}
if(swap)
{
positionsToDraw.push_back(positionsToDraw[0]);
positionsToDraw.erase(positionsToDraw.begin());
}
}
else
{
long controlWire = targets[0];
std::sort(targets.begin(), targets.end());
for(size_t i = 0; i < targets.size(); i++)
{
gridPosition p = getWirePositionInGrid(targets[i]);
if(targets[i] == controlWire)
{
p.isControl = true;
}
positionsToDraw.push_back(p);
}
}
// /*
// * search for min row
// */
// long minRow = LONG_MAX;
// for(size_t i = 0; i < positionsToDraw.size(); i++)
// {
// if(minRow > positionsToDraw[i].gridRow)
// {
// minRow = positionsToDraw[i].gridRow;
// }
// }
//
// //make minRow minus one, to be sure
// long gotoMinRow = minRow;
//// if(positionsToDraw.size() > 2)
// gotoMinRow--;
//
// if(minRow != positionsToDraw.front().gridRow || positionsToDraw.front().isEvenGridColumn())
// {
// //daca merge in jos
// if(positionsToDraw.front().isEvenGridColumn())
// {
// //cel din dreapta cea mai de jos
// gridPosition np;
// np.isFakeTarget = true;
// np.gridRow = gotoMinRow;
// np.gridColumn = positionsToDraw.front().gridColumn - 1;
// positionsToDraw.insert(positionsToDraw.begin(), np);
// }
// else
// {
// //cel de cel mai jos
// gridPosition np;
// np.isFakeTarget = true;
// np.gridRow = gotoMinRow;
// np.gridColumn = positionsToDraw.front().gridColumn;
// positionsToDraw.insert(positionsToDraw.begin(), np);
// }
// }
//
// if(minRow != positionsToDraw.back().gridRow || !positionsToDraw.back().isEvenGridColumn())
// {
// //daca merge in sus
// if(!positionsToDraw.back().isEvenGridColumn())
// {
// gridPosition np;
// np.isFakeTarget = true;
// np.gridRow = gotoMinRow;
// np.gridColumn = positionsToDraw.back().gridColumn + 1;
// positionsToDraw.push_back(np);
// }
// else
// {
// gridPosition np;
// np.isFakeTarget = true;
// np.gridRow = gotoMinRow;
// np.gridColumn = positionsToDraw.back().gridColumn;
// positionsToDraw.push_back(np);
// }
// }
return positionsToDraw;
}
#define NEWCNOT 0
#if NEWCNOT == 1
int circuitgeometry::drawCtrlTgt(gridPosition& position0,
bool previousWasDirectlyOnThisColumn,
bool moveDirectlyOnThisColumn,
bool changeColumn)
{
int widthOffset = 0;
bool isControlWire = position0.isControl;
int sign0 = position0.isEvenGridColumn() ? -1 : 1;
if (isControlWire)
{
if(previousWasDirectlyOnThisColumn)
{
//pleaca din centru si nu de pe margine
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, sign0 * DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, -DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, -sign0 * DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITDEPTH, DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, sign0 * DELTA);
if(!moveDirectlyOnThisColumn && !changeColumn)
{
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, -DELTA);
}
else
{
widthOffset = DELTA;
}
}
else
{
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, sign0 * DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, -sign0 * DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITDEPTH, DELTA);
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, sign0 * DELTA);
if(!moveDirectlyOnThisColumn && !changeColumn)
{
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, -DELTA);
}
else
{
widthOffset = DELTA;
}
}
}
else
{
if(position0.isFakeTarget)
{
//fake targets are either the first or the last
if(previousWasDirectlyOnThisColumn)
{
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, -DELTA);
//go in the direction of the column
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, sign0 * DELTA);
if(moveDirectlyOnThisColumn)
{
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, DELTA);
widthOffset = DELTA;
}
}
else
{
//do nothing, just go in the direction of the column
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, sign0 * DELTA);
if(moveDirectlyOnThisColumn)
{
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, DELTA);
widthOffset = DELTA;
}
}
}
else
{
if(!previousWasDirectlyOnThisColumn)
{
//it's on the rhs of primal defect pair
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, DELTA);
}
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, sign0 * DELTA);
if (changeColumn)
{
//schimba coloana
//e deja cu un DELTA in fata, din cauza ca ramane
widthOffset = DELTA;
}
if(!moveDirectlyOnThisColumn && !changeColumn)
{
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, -DELTA);
}
}
}
return widthOffset;
}
void circuitgeometry::checkAndMoveBackOnWidth(
bool& moveBackToNormalWidthAfterAControl, bool& isControl, long movementFromLastControlPosition)
{
if (moveBackToNormalWidthAfterAControl)
{
if (isControl && (movementFromLastControlPosition != 0))
{
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITDEPTH, -DELTA);
moveBackToNormalWidthAfterAControl = false;
}
if (!isControl)
{
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITDEPTH, -DELTA);
moveBackToNormalWidthAfterAControl = false;
}
}
}
/*
* Works only for single target CNOT? Not general?
*/
void circuitgeometry::addCnotDual(std::vector<long>& targets, long level, bool constructFirstSegment)
{
resetMinMaxCoords();
// long controlWire = targets[0];
std::vector<gridPosition> positionsToDraw = computePositionsForCnotDual(targets);
gridPosition position0 = positionsToDraw[0];
gridPosition position1 = positionsToDraw[1];
int sign0 = position0.isEvenGridColumn() ? -1 : 1;
int sign1 = position1.isEvenGridColumn() ? -1 : 1;
/**
* THE LOOP
*/
bool previousWasDirectlyOnThisColumn = !position0.isFakeTarget;//true;
//2 points for control: c1 the left, c2 the right
convertcoordinate c1;
//daca previousWasDirectlyOnThisColumn == true atunci minus 0, altfel minus 1
c1[CIRCUITWIDTH] = (position0.gridColumn * 2 - (previousWasDirectlyOnThisColumn ? 0 : 1)) * DELTA + getPrevHalfDelta(1);
c1[CIRCUITDEPTH] = level * getDepthShift() - getPrevHalfDelta(-1);
c1[CIRCUITHEIGHT] = position0.gridRow * DELTA + getPrevHalfDelta(1);
if(sign0 == 1)
{
c1[CIRCUITHEIGHT] -= DELTA;
}
convertcoordinate c2(c1);
// c2[CIRCUITDEPTH] += getDepthShift();
// lastDualIndices[CURR1] = simplegeom.addCoordinate(c1);
lastDualIndices[CURR2] = simplegeom.addCoordinate(c2);
int firstIndex = lastDualIndices[CURR2];
// simplegeom.addSegment(lastDualIndices[CURR1], lastDualIndices[CURR2]);
// if(targets[0] == controlWire)
// {
// liDualAddAxisOffsetAndAddSegment(CURR1, CIRCUITWIDTH, -DELTA);
// }
gridPosition distance;
distance.gridRow = position1.gridRow - position0.gridRow;
distance.gridColumn = position1.gridColumn - position0.gridColumn;
//for(int i = 0; i < targets.size(); i++)
bool moveBackToNormalWidthAfterAControl = false;
for(size_t i = 0; i < positionsToDraw.size(); i++)
{
//place the first element
position0 = positionsToDraw[i];//getWirePositionInGrid(targets[i]);
if (i == positionsToDraw.size() - 1)
{
position1 = position0;
// position1 = positionsToDraw[0];
//TODO: this does not look good when drawn - keeps the control movement on the right side
}
else
{
position1 = positionsToDraw[i + 1];//getWirePositionInGrid(targets[i + 1]);
}
sign0 = position0.isEvenGridColumn() ? -1 : 1;
sign1 = position1.isEvenGridColumn() ? -1 : 1;
distance.gridRow = position1.gridRow - position0.gridRow;
distance.gridColumn = position1.gridColumn - position0.gridColumn;
int signGridRow = 1;
if(distance.gridRow != 0)
{
signGridRow = abs(distance.gridRow) / distance.gridRow;
}
int signGridColumn = 1;
if(distance.gridColumn!= 0)
{
signGridColumn = abs(distance.gridColumn) / distance.gridColumn;
}
bool nextElementIsRightBelow = (signGridRow == sign0) && distance.gridColumn == 0 && abs(distance.gridRow) == 1;
bool changeColumnImmediately = (signGridRow != sign0) || (distance.gridRow == 0);
/*
* Aici se mai leaga inca de targets
*/
//isControl should be removed with position0.isControl ?
bool isControl = position0.isControl;
//if the movement back was not performed, maintain the flag
moveBackToNormalWidthAfterAControl = moveBackToNormalWidthAfterAControl ? true : isControl;
int widthOffset = drawCtrlTgt(position0, previousWasDirectlyOnThisColumn, nextElementIsRightBelow, changeColumnImmediately);
previousWasDirectlyOnThisColumn = nextElementIsRightBelow;
/*
* the control changes the depth of the line
* after the first non-zero width or height change go back with depth -DELTA
*/
//parcurge distanta pe coloana care se potriveste
if(!changeColumnImmediately)
{
//advance on this column
int offset3 = 1;
if(distance.gridColumn != 0)
{
offset3 = 0;
}
long move1 = (distance.gridRow - sign0 * offset3) * DELTA;
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, move1);
checkAndMoveBackOnWidth(moveBackToNormalWidthAfterAControl,
isControl, move1);
// //AICI ESTE AL DOILEA ELEMENT
if(distance.gridColumn != 0)
{
//advance on next column
long move2 = 2*DELTA - widthOffset;
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, move2);
checkAndMoveBackOnWidth(moveBackToNormalWidthAfterAControl,
isControl, move2);
}
}
else
{
//advance on next column
//first move to next column
long move3 = 2*DELTA - widthOffset;
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, move3);
checkAndMoveBackOnWidth(moveBackToNormalWidthAfterAControl,
isControl, move3);
//move into the direction
//changed sign0, because of next column
long move4 = (distance.gridRow - 0) * DELTA;
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, move4);
checkAndMoveBackOnWidth(moveBackToNormalWidthAfterAControl,
isControl, move4);
}
if(distance.gridColumn != 0)
{
if(sign0 == sign1)
{
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, -sign1 * DELTA);
}
liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, 2*(distance.gridColumn - 1) * DELTA);
}
}
// close loop
// if(sign1*simplegeom.coords[lastDualIndices[CURR2]][CIRCUITHEIGHT] <= sign1*heightToCompareAtEnd)
// {
// long offoff = sign1*heightToCompareAtEnd - sign1*simplegeom.coords[lastDualIndices[CURR2]][CIRCUITHEIGHT];
// offoff = sign1*offoff + sign1 * DELTA;
// liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITHEIGHT, offoff);
// }
//
// long offset = simplegeom.coords[lastDualIndices[CURR2]][CIRCUITWIDTH];
// offset = offset - minCoords[CIRCUITWIDTH];
// liDualAddAxisOffsetAndAddSegment(CURR2, CIRCUITWIDTH, -offset);
//
// simplegeom.addSegment(firstIndex, lastDualIndices[CURR2]);
}
#endif
#if NEWCNOT == 0
convertcoordinate circuitgeometry::addLoopAround(int coordsIndexPos, int direction, convertcoordinate& startcoord)
{
int idx = simplegeom.addCoordinate(startcoord);
lastDualIndices[coordsIndexPos] = idx;
liDualAddAxisOffsetAndAddSegment2(coordsIndexPos, CIRCUITWIDTH, -DELTA);
liDualAddAxisOffsetAndAddSegment2(coordsIndexPos, CIRCUITHEIGHT, direction * DELTA);
liDualAddAxisOffsetAndAddSegment2(coordsIndexPos, CIRCUITWIDTH, DELTA);
return simplegeom.coords[lastDualIndices[coordsIndexPos]];
}
convertcoordinate circuitgeometry::addGoThrough(int coordsIndexPos, int direction, convertcoordinate& startcoord)
{
int idx = simplegeom.addCoordinate(startcoord);
lastDualIndices[coordsIndexPos] = idx;
liDualAddAxisOffsetAndAddSegment2(coordsIndexPos, CIRCUITHEIGHT, direction * DELTA);
return simplegeom.coords[lastDualIndices[coordsIndexPos]];
}
convertcoordinate circuitgeometry::moveToGridPosition(int coordsIndexPos, gridPosition& current, gridPosition& target, convertcoordinate& start)
{
gridPosition distance;
distance.gridRow = target.gridRow - current.gridRow;
distance.gridColumn = target.gridColumn - current.gridColumn;
int signGridRow = 1;
if(distance.gridRow != 0)
{
signGridRow = abs(distance.gridRow) / distance.gridRow;
}
int signCurrent = current.isEvenGridColumn() ? -1 : 1;//0
int signTarget = target.isEvenGridColumn() ? -1 : 1;//1
int idx = simplegeom.addCoordinate(start);
lastDualIndices[coordsIndexPos] = idx;
//parcurge distanta pe coloana care se potriveste
if(signGridRow == signCurrent)
{
int offset3 = 0;
if(distance.gridColumn == 0)
{
offset3 = 1;
}
//advance on this column
liDualAddAxisOffsetAndAddSegment2(coordsIndexPos, CIRCUITHEIGHT, (distance.gridRow - signCurrent * offset3) * DELTA);
//AICI ESTE AL DOILEA ELEMENT
if(distance.gridColumn != 0)
{
//advance on next column
liDualAddAxisOffsetAndAddSegment2(coordsIndexPos, CIRCUITWIDTH, 2*DELTA);
}
}
else
{
//advance on next column
//first move to next column
//TODO: THERE IS A BUG HERE....: face TARGET TOATA COLOANA. trebuie verificat din nou
liDualAddAxisOffsetAndAddSegment2(coordsIndexPos, CIRCUITWIDTH, 2*DELTA);
//move into the direction
//changed sign0, because of next column
liDualAddAxisOffsetAndAddSegment2(coordsIndexPos, CIRCUITHEIGHT, distance.gridRow * DELTA);
}
if(distance.gridColumn != 0)
{
if(signCurrent == signTarget)
{
liDualAddAxisOffsetAndAddSegment2(coordsIndexPos, CIRCUITHEIGHT, -signTarget * DELTA);
}
liDualAddAxisOffsetAndAddSegment2(coordsIndexPos, CIRCUITWIDTH, 2*(distance.gridColumn - 1) * DELTA);
}
return simplegeom.coords[lastDualIndices[coordsIndexPos]];
}
void circuitgeometry::addCnotDual(std::vector<long>& targets, long level, bool constructFirstSegment)
{