PauliOperatorTester.cpp 14.9 KB
Newer Older
Mccaskey, Alex's avatar
Mccaskey, Alex committed
1
2
3
4
5
6
7
8
/*******************************************************************************
 * Copyright (c) 2019 UT-Battelle, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompanies this
 * distribution. The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
 *License is available at https://eclipse.org/org/documents/edl-v10.php
9
10
 *
 * Contributors:
Mccaskey, Alex's avatar
Mccaskey, Alex committed
11
12
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
13
14
#include <gtest/gtest.h>
#include "PauliOperator.hpp"
15
#include "xacc.hpp"
16
17
18
19
20
#include "IRProvider.hpp"
#include <Eigen/Dense>
#include "xacc_service.hpp"

using namespace xacc::quantum;
21
TEST(PauliOperatorTester, checkSpMat) {
22

23
  PauliOperator op("X0 + Z0");
24

25
  auto sp_mat = op.to_sparse_matrix();
26

27
28
29
  for (auto &s : sp_mat) {
    std::cout << s.coeff() << ", " << s.row() << ", " << s.col() << "\n";
  }
30

31
32
33
34
35
36
  PauliOperator op2(
      "(0.1202,0) Z2 Z3 + (0.174073,0) Z1 Z3 + (0.0454063,0) Y0 Y1 Y2 Y3 + "
      "(0.17028,0) Z0 + (0.0454063,0) X0 X1 Y2 Y3 + (-0.220041,0) Z1 + "
      "(0.165607,0) Z1 Z2 + (0.165607,0) Z0 Z3 + (0.17028,0) Z2 + "
      "(0.0454063,0) X0 X1 X2 X3 + (-0.106477,0) + (0.1202,0) Z0 Z1 + "
      "(0.0454063,0) Y0 Y1 X2 X3 + (0.168336,0) Z0 Z2 + (-0.220041,0) Z3");
37

38
39
40
41
  auto s = op2.to_sparse_matrix();
  for (auto &ss : s) {
    std::cout << ss.coeff() << ", " << ss.row() << ", " << ss.col() << "\n";
  }
42
}
43
TEST(PauliOperatorTester, checkEasy) {
44

45
46
  //	CURRENT: (0.25,0) X1 Z2 X3 + (0,0.25) X1 Z2 Y3 + (0.25,0) Y1 Z2 Y3 +
  //(0,-0.25) Y1 Z2 X3 	SUM: (0,-0.5) Z0 Y1 + (0.5,0) Z0 X1
47

48
49
50
51
52
53
54
  PauliOperator op({{1, "X"}, {2, "Z"}, {3, "X"}}, .25);
  op += PauliOperator({{1, "X"}, {2, "Z"}, {3, "Y"}},
                      std::complex<double>(0, .25));
  op += PauliOperator({{1, "Y"}, {2, "Z"}, {3, "Y"}}, .25);
  op += PauliOperator({{1, "Y"}, {2, "Z"}, {3, "X"}},
                      std::complex<double>(0, -.25));
  EXPECT_EQ(4, op.nQubits());
55

56
57
  PauliOperator sum({{0, "Z"}, {1, "Y"}}, std::complex<double>(0, -.5));
  sum += PauliOperator({{0, "Z"}, {1, "X"}}, .5);
58

59
60
  std::cout << "PRINT: " << op.toString() << "\n";
  std::cout << "SUM: " << sum.toString() << "\n";
61

62
  op *= sum;
63

64
  std::cout << "CURRENTMULT: " << op.toString() << "\n";
65

66
  EXPECT_EQ(0, op.nQubits());
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
TEST(PauliOperatorTester, toDenseMatrix) {
  PauliOperator op({{0, "X"}});
  auto data = op.toDenseMatrix(1);
  Eigen::MatrixXcd m = Eigen::Map<Eigen::MatrixXcd>(data.data(), 2, 2);
  std::cout << "HELLO: " << m << "\n";
}
TEST(PauliOperatorTester, checkConstruction) {

  PauliOperator inst({{4, "X"}, {3, "Z"}, {9, "Y"}, {1, "Z"}});

  std::cout << "PRINT: " << inst.toString() << "\n";

  PauliOperator i2({{4, "X"}, {3, "Z"}});
  auto sumInst = inst + i2;
  std::cout << "PRINT: " << sumInst.toString() << "\n";

  PauliOperator expected({{3, "Z"}, {4, "X"}});
  expected += PauliOperator({{1, "Z"}, {3, "Z"}, {4, "X"}, {9, "Y"}});
  std::cout << "EXPECTED: " << expected.toString() << "\n";
  EXPECT_TRUE(expected == sumInst);
  EXPECT_TRUE(sumInst == sumInst);

  PauliOperator i3({{3, "X"}});
  auto testPauliProducts = i2 * i3;
  std::cout << testPauliProducts.toString() << "\n";

  expected.clear();
  expected += PauliOperator({{3, "Y"}, {4, "X"}}, std::complex<double>(0, 1));
  //	expected.addTerm({{3,"Y"},{4,"X"}}, std::complex<double>(0,1));

  std::cout << "NEWEXPECTED: " << expected.toString() << "\n";
  EXPECT_TRUE(expected == testPauliProducts);

  PauliOperator testEqual({{4, "X"}, {3, "Z"}},
                          std::complex<double>(33.3, 0.0));
  EXPECT_TRUE(i2 == testEqual);
  EXPECT_TRUE(i2 != inst);

  // Term * scalar multiple
  auto multScalar = testPauliProducts * 3.3;
  EXPECT_TRUE("(0,3.3) Y3 X4" == multScalar.toString());
  EXPECT_TRUE(PauliOperator({{3, "Y"}, {4, "X"}},
                            std::complex<double>(0, 3.3)) == multScalar);

  testPauliProducts *= 3.3;
  EXPECT_TRUE(testPauliProducts == multScalar);

  // Plus with same terms
  sumInst = inst + 2.2 * inst;
  std::cout << inst.toString() << ", " << (2.2 * inst).toString() << ", "
            << sumInst.toString() << "\n";
  EXPECT_TRUE(PauliOperator({{1, "Z"}, {3, "Z"}, {4, "X"}, {9, "Y"}},
                            std::complex<double>(3.2, 0)) == sumInst);

  PauliOperator i4({{0, "Z"}});
  PauliOperator i6({{1, "Y"}}, std::complex<double>(0, 1));
  i6 += PauliOperator({{1, "X"}});
  PauliOperator i7({{1, "Z"}});

  expected.clear();
  expected += PauliOperator({{0, "Z"}, {1, "Y"}}, std::complex<double>(0, 1));
  expected += PauliOperator({{0, "Z"}, {1, "X"}});
  //	expected.addTerm({{0, "Z"}, {1, "Y"}}, std::complex<double>(0,1));
  //	expected.addTerm({{0, "Z"}, {1, "X"}});

  std::cout << i4.toString() << ", " << i6.toString() << "\nSTARTING:\n";
  auto newMultByComp = i4 * i6;
  std::cout << "ENDED:\nHI: " << newMultByComp.toString() << "\n";
  EXPECT_TRUE(expected == newMultByComp);

  std::cout << "MADE IT HERE\n";
  // Z1 * X1 + i * Z1 * Y1 = i * Y1 + i * (-i * X1)
  newMultByComp = i7 * i6;
  expected.clear();
  expected += PauliOperator({{1, "X"}}) +
              PauliOperator({{1, "Y"}}, std::complex<double>(0, 1));
  //	expected.addTerm({{1,"X"}}).addTerm({{1,"Y"}},
  //std::complex<double>(0,1));

  EXPECT_TRUE(expected == newMultByComp);
149
150
}

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
TEST(PauliOperatorTester, checkVariableCoefficient) {
  // make a4dag a3dag a9 a1
  PauliOperator inst(
      {
          {4, "X"},
          {3, "Z"},
      },
      "theta");
  auto three = 3 * inst;
  auto sum = ((inst + inst) + three);
  std::cout << "SUMSTR: " << sum.toString() << "\n";
  EXPECT_TRUE(sum.toString() == "(5,0) theta Z3 X4");

  PauliOperator i2({{4, "X"}, {3, "Z"}}, "theta2");

  EXPECT_TRUE(i2 != inst);
  auto sumInst = inst + i2;
  std::cout << "SUMSTR: " << sumInst.toString() << "\n";
  EXPECT_TRUE("(1,0) theta2 Z3 X4 + (1,0) theta Z3 X4" == sumInst.toString());
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
TEST(PauliOperatorTester, checkBinaryVector) {
  //	SpinInstruction inst( { { 4, "X" },
  //				{ 3, "Z" }, { 9, "Y" }, { 1, "Z" } });
  //
  //	// This is Z1 Z3 X4 Y9
  //
  //	auto bv = inst.toBinaryVector(10);
  //
  //	EXPECT_TRUE(bv.size() == 20);
  //
  //	std::vector<int> expected(20);
  //	expected[4] = 1;
  //	expected[9] = 1;
  //	expected[19] = 1;
  //	expected[13] = 1;
  //	expected[11] = 1;
  //
  //	EXPECT_TRUE(expected == bv);
  //
  //	SpinInstruction i;
  //	std::vector<int> zeros(20);
  //	EXPECT_TRUE(zeros == i.toBinaryVector(10));
  //
  //	i.fromBinaryVector(expected, std::complex<double>(1.0,0.0));
  //
  //	EXPECT_TRUE(inst.toString() == i.toString());
}
199

200
TEST(PauliOperatorTester, checkAction) {
201

202
203
  PauliOperator inst({{0, "X"}, {1, "Z"}, {2, "Y"}, {3, "Z"}},
                     std::complex<double>(2.2, 0));
204

205
  auto result = inst.computeActionOnKet("0110");
206

207
208
  EXPECT_TRUE(result[0].first == "1100");
  EXPECT_TRUE(result[0].second == std::complex<double>(0, 2.2));
209

210
211
  std::cout << "HELLO:\n"
            << result[0].first << ", " << result[0].second << "\n";
212

213
214
  PauliOperator inst2({{0, "X"}, {1, "Z"}, {2, "Y"}, {3, "Y"}},
                      std::complex<double>(2.2, 0));
215

216
  auto result2 = inst2.computeActionOnKet("0110");
217

218
219
  std::cout << "HELLO:\n"
            << result2[0].first << ", " << result2[0].second << "\n";
220

221
222
  EXPECT_TRUE(result2[0].first == "1101");
  EXPECT_TRUE(result2[0].second == std::complex<double>(-2.2, 0));
223
224
}

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
TEST(PauliOperatorTester, checkComplexTerms) {
  //	SpinInstruction inst({{1,"X"}, {3,"Y"}, {8,"Z"}},
  //std::complex<double>(0.5,0.0)), 			inst2({{1,"Z"}, {3,"X"}, {8,"Z"}},
  //std::complex<double>(1.2,0.0)), 			inst3({{1,"Z"}, {3,"Y"}, {9,"Z"}},
  //std::complex<double>(0.0,1.4)), 			c({}, std::complex<double>(.25+1.2*1.2, 0.0)
  //					+
  //std::complex<double>(0,1.4)*std::complex<double>(0,1.4)),
  //			c2({{1,"Y"},{3,"Z"}},
  //std::complex<double>(0,2)*std::complex<double>(0,1)*std::complex<double>(.5,0)*std::complex<double>(1.2,0));
  //
  //	auto op = inst+inst2+inst3;
  //
  //	auto res = op*op;
  //	std::cout << "RES: " << res.toString("") << "\n";
  //	res.simplify();
  //	std::cout << "Simplified: " << res.toString("") << "\n";
  //
  //	auto t = c + c2;
  //	t.simplify();
  //	std::cout << "TSimp: " << t.toString("") << "\n";
  //
  //	EXPECT_TRUE(t == res);
247
248
}

249
TEST(PauliOperatorTester, checkHelpMe) {
250

251
252
253
254
  PauliOperator sum1({{0, "X"}}, std::complex<double>(.5, 0));
  PauliOperator sum2({{0, "Y"}}, std::complex<double>(0, 0.5));
  PauliOperator current1({{0, "X"}}, std::complex<double>(-.62215, 0));
  PauliOperator current2({{0, "Y"}}, std::complex<double>(0, 0.62215));
255

256
257
258
  PauliOperator sum, current;
  sum += sum1 + sum2;
  current += current1 + current2;
259

260
261
  //	UM: (0.5,0) * X0 + (0,0.5) * Y0
  //	Current: (-0.62215,0) * X0 + (0,0.62215) * Y0
262

263
264
265
  std::cout << "SUM: " << sum.toString() << "\n";
  std::cout << "Current: " << current.toString() << "\n";
  auto x = current * sum;
266

267
  std::cout << "RESULT: " << x.toString() << "\n";
268

269
270
  std::cout << (std::complex<double>(1, 0) * std::complex<double>(0, 1))
            << "\n";
271
272
}

273
274
275
276
277
TEST(PauliOperatorTester, checkIdentity) {
  PauliOperator i1(std::complex<double>(3.3, 0));
  PauliOperator i2(std::complex<double>(5.3, 0));
  PauliOperator compInst;
  compInst += i1;
278

279
  compInst = compInst + i2;
280

281
282
283
  std::cout << "HI: " << compInst.toString() << "\n";
  EXPECT_TRUE("(8.6,0)" == compInst.toString());
  EXPECT_TRUE(PauliOperator(std::complex<double>(8.6, 0)) == compInst);
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
TEST(PauliOperatorTester, checkComposition) {
  PauliOperator i1({{0, "Z"}});
  PauliOperator i2({{1, "X"}});
  PauliOperator i3({{1, "Y"}}, std::complex<double>(0, 1));
  PauliOperator i4({{1, "Z"}});
  PauliOperator i5({{1, "Y"}}, std::complex<double>(0, -1));

  PauliOperator compInst, compInst2, compInst3;
  compInst += i2 + i3;

  compInst3 += i2 + i1;

  compInst2 += i2;

  std::complex<double> i(0, 1);
  EXPECT_TRUE(compInst == compInst);
  EXPECT_TRUE(compInst != compInst2);
  EXPECT_TRUE(compInst2 == i2);
  EXPECT_TRUE(compInst2 != i1);

  auto multBySpinInst = compInst * i4;
  PauliOperator expected({{1, "X"}}, -1);
  expected += PauliOperator({{1, "Y"}}, -i);
  EXPECT_TRUE(expected == multBySpinInst);

  multBySpinInst = compInst * i1;
  std::cout << "HOWDY: " << multBySpinInst.toString() << "\n";
  expected.clear();
  expected += PauliOperator({{0, "Z"}, {1, "Y"}}, i);
  expected += PauliOperator({{0, "Z"}, {1, "X"}});
  EXPECT_TRUE(expected == multBySpinInst);

  auto multByComposite = compInst * compInst;
  std::cout << "ZERO: " << multByComposite.toString() << "\n";
  EXPECT_TRUE(PauliOperator() == multByComposite);

  auto t = compInst3 * compInst3;
  expected.clear();
  expected += PauliOperator({{0, "Z"}, {1, "X"}}, 2);
  expected += PauliOperator(2);
  EXPECT_TRUE(expected == t);

  auto test = compInst * 4.4;
  expected.clear();
  expected += PauliOperator({{1, "X"}}, 4.4);
  expected += PauliOperator({{1, "Y"}}, 4.4 * i);
  EXPECT_TRUE(expected == test);

  EXPECT_TRUE(expected == (4.4 * compInst));

  test = compInst * std::complex<double>(4.4, 0);
  EXPECT_TRUE(expected == test);
  EXPECT_TRUE(expected == (std::complex<double>(4.4, 0) * compInst));

  // Test Addition now

  auto added = compInst + compInst3;
  expected.clear();
  expected += PauliOperator({{0, "Z"}});
  expected += PauliOperator({{1, "X"}}, 2);
  expected += PauliOperator({{1, "Y"}}, i);

  EXPECT_TRUE(expected == added);
349
350
}

351
352
353
TEST(PauliOperatorTester, checkMatrixElements) {
  PauliOperator op({{0, "X"}, {1, "Y"}, {2, "Z"}});
  auto elements = op.getSparseMatrixElements();
354
355
}

356
TEST(PauliOperatorTester, checkFromXACCIR) {
357

358
  using namespace xacc;
359

360
361
362
  auto gateRegistry = xacc::getService<IRProvider>("quantum");
  auto f = gateRegistry->createComposite("f");
  auto f2 = gateRegistry->createComposite("f2");
363

364
365
366
367
368
369
370
  auto h1 = gateRegistry->createInstruction("H", 0);
  auto h2 = gateRegistry->createInstruction("H", 1);
  auto m1 = gateRegistry->createInstruction("Measure", 0);
  // InstructionParameter p(0), p1(1), p2(2);
  // m1->setParameter(0, p);
  auto m2 = gateRegistry->createInstruction("Measure", 1);
  // m2->setParameter(0,p1);
371

372
373
  auto m3 = gateRegistry->createInstruction("Measure", 2);
  // m3->setParameter(0,p2);
374

375
376
377
378
379
  f->addInstruction(h1);
  f->addInstruction(h2);
  f->addInstruction(m1);
  f->addInstruction(m2);
  f->addInstruction(m3);
380

381
382
383
384
  auto h3 = gateRegistry->createInstruction("H", 0);
  auto rx = gateRegistry->createInstruction("Rx", 1);
  InstructionParameter q(xacc::constants::pi / 2.0);
  rx->setParameter(0, q);
385

386
387
388
  auto m4 = gateRegistry->createInstruction("Measure", 0);
  auto m5 = gateRegistry->createInstruction("Measure", 1);
  auto m6 = gateRegistry->createInstruction("Measure", 2);
389

390
391
392
393
394
  f2->addInstruction(h3);
  f2->addInstruction(rx);
  f2->addInstruction(m4);
  f2->addInstruction(m5);
  f2->addInstruction(m6);
395

396
397
398
  auto ir = gateRegistry->createIR();
  ir->addComposite(f);
  ir->addComposite(f2);
399

400
  PauliOperator op;
401

402
  op.fromXACCIR(ir);
403

404
  std::cout << "HEY: " << op.toString() << "\n";
405
406
}

407
TEST(PauliOperatorTester, checkFromXACCIR2) {
408

409
410
  PauliOperator op;
  op.fromString("I + Z0 Z1 + X0 X1 + Y0 Y1 + Z0 + Z1");
411

412
413
  std::cout << "OP ON OUR SIDE IS " << op.toString() << "\n";
  auto measureFunctions = op.toXACCIR()->getComposites();
414

415
416
417
  auto ir = xacc::getService<xacc::IRProvider>("quantum")->createIR();
  for (auto &f : measureFunctions) {
    ir->addComposite(f);
418
419
420
421
422
423
424
  }
  PauliOperator H;
  H.fromXACCIR(ir);
  std::cout << "H is\n" << H.toString() << "\n";

  EXPECT_TRUE(H == op);
}
425
426
TEST(PauliOperatorTester, checkFromStr) {
  const std::string s = "(-.00944179,0) Z1 X2 X3 + 0.0816923 Z2 Z3";
427

428
429
  PauliOperator op;
  op.fromString(s);
430

431
432
433
  std::cout << op.toString() << "\n";
  PauliOperator expected({{1, "Z"}, {2, "X"}, {3, "X"}}, -0.00944179);
  expected += PauliOperator({{2, "Z"}, {3, "Z"}}, 0.0816923);
434

435
  EXPECT_TRUE(op == expected);
436

437
438
439
440
  auto s2 = "X0X1";
  PauliOperator op2("X0X1");
  //    op2.fromString(s2);
  std::cout << "Op2: " << op2.toString() << "\n";
441

442
443
  PauliOperator op3("5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1");
  std::cout << "Op3: " << op3.toString() << "\n";
444
445
}

446
447
448
449
TEST(PauliOperatorTester, checkContainsTerm) {
  const std::string s = "(-.00944179,0) Z1 X2 X3 + (0.0816923,0) Z2 Z3";
  PauliOperator op;
  op.fromString(s);
450

451
452
  PauliOperator subOp({{1, "Z"}, {2, "X"}, {3, "X"}}, -0.00944179);
  PauliOperator subOp2({{2, "X"}, {3, "X"}}, -0.00944179);
453

454
455
  EXPECT_TRUE(op.contains(subOp));
  EXPECT_FALSE(op.contains(subOp2));
456
457
}

458
459
460
TEST(PauliOperatorTester, checkCommutes) {
  PauliOperator op;
  op.fromString("I + Z0 + Z1 + X0 X1 + Y0 Y1 + Z0 Z1");
461

462
463
  PauliOperator zz({{0, "Z"}, {1, "Z"}});
  PauliOperator y({{0, "Y"}});
464

465
466
  EXPECT_TRUE(op.commutes(zz));
  EXPECT_FALSE(PauliOperator({{0, "X"}}).commutes(y));
467
}
468
469
470
471
472
473
474
475
476

TEST(PauliOperatorTester, checkSciNot) {
  PauliOperator op;
  op.fromString("(1.234e-4, 0) Z0");

  std::cout << op.toString() << "\n";

}

477
478
479
480
481
482
int main(int argc, char **argv) {
  xacc::Initialize(argc, argv);
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  xacc::Finalize();
  return ret;
483
}