CircuitOptimizerTester.cpp 25.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*******************************************************************************
 * Copyright (c) 2017 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
 *
 * Contributors:
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
#include "InstructionIterator.hpp"
#include <gtest/gtest.h>
#include "Compiler.hpp"
// #include "CircuitOptimizer.hpp"
#include "CountGatesOfTypeVisitor.hpp"
#include "CommonGates.hpp"

20
#include "xacc.hpp"
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
#include "xacc_service.hpp"
#include "IRTransformation.hpp"

using namespace xacc;
using namespace xacc::quantum;
const std::string uccsdSrc = R"uccsdSrc(__qpu__ void foo77(qbit q, double theta0, double theta1) {
   X(q[0]);
   X(q[1]);
   H(q[3]);
   Rx(q[1], pi/2);
   CNOT(q[1], q[2]);
   CNOT(q[2], q[3]);
   Rz(q[3], theta0);
   CNOT(q[2], q[3]);
   CNOT(q[1], q[2]);
   H(q[3]);
   Rx(q[1], -pi/2);
   Rx(q[3], pi/2);
   H(q[1]);
   CNOT(q[1], q[2]);
   CNOT(q[2], q[3]);
   Rz(q[3], -theta0);
   CNOT(q[2], q[3]);
   CNOT(q[1], q[2]);
   Rx(q[3], -pi/2);
   H(q[1]);
   H(q[3]);
   Rx(q[2], pi/2);
   H(q[1]);
   H(q[0]);
   CNOT(q[0], q[1]);
   CNOT(q[1], q[2]);
   CNOT(q[2], q[3]);
   Rz(q[3], theta1/2);
   CNOT(q[2], q[3]);
   CNOT(q[1], q[2]);
   CNOT(q[0], q[1]);
   H(q[3]);
   Rx(q[2], -pi/2);
   H(q[1]);
   H(q[0]);
   Rx(q[3], pi/2);
   Rx(q[2], pi/2);
   Rx(q[1], pi/2);
   H(q[0]);
   CNOT(q[0], q[1]);
   CNOT(q[1], q[2]);
   CNOT(q[2], q[3]);
   Rz(q[3], theta1/2);
   CNOT(q[2], q[3]);
   CNOT(q[1], q[2]);
   CNOT(q[0], q[1]);
   Rx(q[3], -pi/2);
   Rx(q[2], -pi/2);
   Rx(q[1], -pi/2);
   H(q[0]);
   H(q[3]);
   H(q[2]);
   H(q[1]);
   Rx(q[0], pi/2);
   CNOT(q[0], q[1]);
   CNOT(q[1], q[2]);
   CNOT(q[2], q[3]);
   Rz(q[3], -theta1/2);
   CNOT(q[2], q[3]);
   CNOT(q[1], q[2]);
   CNOT(q[0], q[1]);
   H(q[3]);
   H(q[2]);
   H(q[1]);
   Rx(q[0], -pi/2);
   Rx(q[3], pi/2);
   H(q[2]);
   Rx(q[1], pi/2);
   Rx(q[0], pi/2);
   CNOT(q[0], q[1]);
   CNOT(q[1], q[2]);
   CNOT(q[2], q[3]);
   Rz(q[3], -theta1/2);
   CNOT(q[2], q[3]);
   CNOT(q[1], q[2]);
   CNOT(q[0], q[1]);
   Rx(q[3], -pi/2);
   H(q[2]);
   Rx(q[1], -pi/2);
   Rx(q[0], -pi/2);
   Rx(q[3], pi/2);
   H(q[2]);
   H(q[1]);
   H(q[0]);
   CNOT(q[0], q[1]);
   CNOT(q[1], q[2]);
   CNOT(q[2], q[3]);
   Rz(q[3], theta1/2);
   CNOT(q[2], q[3]);
   CNOT(q[1], q[2]);
   CNOT(q[0], q[1]);
   Rx(q[3], -pi/2);
   H(q[2]);
   H(q[1]);
   H(q[0]);
   Rx(q[2], pi/2);
   H(q[0]);
   CNOT(q[0], q[1]);
   CNOT(q[1], q[2]);
   Rz(q[2], -theta0);
   CNOT(q[1], q[2]);
   CNOT(q[0], q[1]);
   Rx(q[2], -pi/2);
   H(q[0]);
   H(q[2]);
   Rx(q[0], pi/2);
   CNOT(q[0], q[1]);
   CNOT(q[1], q[2]);
   Rz(q[2], theta0);
   CNOT(q[1], q[2]);
   CNOT(q[0], q[1]);
   H(q[2]);
   Rx(q[0], -pi/2);
   Rx(q[3], pi/2);
   Rx(q[2], pi/2);
   H(q[1]);
   Rx(q[0], pi/2);
   CNOT(q[0], q[1]);
   CNOT(q[1], q[2]);
   CNOT(q[2], q[3]);
   Rz(q[3], theta1/2);
   CNOT(q[2], q[3]);
   CNOT(q[1], q[2]);
   CNOT(q[0], q[1]);
   Rx(q[3], -pi/2);
   Rx(q[2], -pi/2);
   H(q[1]);
   Rx(q[0], -pi/2);
   H(q[3]);
   Rx(q[2], pi/2);
   Rx(q[1], pi/2);
   Rx(q[0], pi/2);
   CNOT(q[0], q[1]);
   CNOT(q[1], q[2]);
   CNOT(q[2], q[3]);
   Rz(q[3], -theta1/2);
   CNOT(q[2], q[3]);
   CNOT(q[1], q[2]);
   CNOT(q[0], q[1]);
   H(q[3]);
   Rx(q[2], -pi/2);
   Rx(q[1], -pi/2);
   Rx(q[0], -pi/2);
   H(q[3]);
   H(q[2]);
   Rx(q[1], pi/2);
   H(q[0]);
   CNOT(q[0], q[1]);
   CNOT(q[1], q[2]);
   CNOT(q[2], q[3]);
   Rz(q[3], -theta1/2);
   CNOT(q[2], q[3]);
   CNOT(q[1], q[2]);
   CNOT(q[0], q[1]);
   H(q[3]);
   H(q[2]);
   Rx(q[1], -pi/2);
   H(q[0]);
   }
   )uccsdSrc";

TEST(CircuitOptimizerTester, checkOptimize) {

    auto c = xacc::getService<xacc::Compiler>("xasm");
    auto f = c->compile(uccsdSrc)->getComposites()[0];
    std::vector<double> p{0.0,0.0};
    auto fevaled = (*f)(p);

195
    // auto provider = xacc::getIRProvider("quantum");
196

197
198
    // auto ir = provider->createIR();
    // ir->addComposite(fevaled);
199
200
201
202

    CountGatesOfTypeVisitor<CNOT> countCx(fevaled);

    auto opt = xacc::getService<IRTransformation>("circuit-optimizer");
203
    opt->apply(fevaled, nullptr);
204

205
206
207
    // optF = optF->enabledView();//std::dynamic_pointer_cast<Circuit>(optF->enabledView());
    CountGatesOfTypeVisitor<CNOT> countCx2(fevaled);
    CountGatesOfTypeVisitor<Rz> countRz(fevaled);
208
209
    EXPECT_EQ(0, countRz.countGates());
    EXPECT_EQ(0, countCx2.countGates());
210
211
212
213
214
    EXPECT_EQ(2, fevaled->nInstructions());
    EXPECT_EQ("X", fevaled->getInstruction(0)->name());
    EXPECT_EQ("X", fevaled->getInstruction(1)->name());
    EXPECT_EQ(std::vector<std::size_t>{0}, fevaled->getInstruction(0)->bits());
    EXPECT_EQ(std::vector<std::size_t>{1}, fevaled->getInstruction(1)->bits());
215

216
    std::cout << "FINAL CIRCUIT:\n" << fevaled->toString() << "\n";
217
218
219

}

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
TEST(CircuitOptimizerTester, checkRemove0StrRotation) {
    auto c = xacc::getService<xacc::Compiler>("xasm");
    auto f = c->compile(R"(__qpu__ void test_rot(qbit q, double x) {
        H(q[2]);
        CX(q[0],q[1]);
        Rx(q[1], 0.0 * x);
        CX(q[0],q[1]);
        Rx(q[1], -pi/2);
        H(q[2]);
    })")
                 ->getComposites()[0];

 auto opt = xacc::getService<IRTransformation>("circuit-optimizer");

    opt->apply(f, nullptr);
     std::cout << "HOWDY:\n" << f->toString() << "\n";
  EXPECT_EQ(1, f->nInstructions());
}

239
240
241
242
243
244
245
246
247
248
249
250
251
TEST(CircuitOptimizerTester, checkAdjRotations) {
    auto c = xacc::getService<xacc::Compiler>("xasm");
    auto f = c->compile(R"(__qpu__ void foo(qbit q) {
        H(q[0]);
        Rx(q[1], pi/2);
        Rx(q[1], -pi/2);
        H(q[2]);
        Rz(q[3], 0.5);
        Rz(q[3], 0.5);
    })")
                 ->getComposites()[0];
     auto provider = xacc::getIRProvider("quantum");

252
    // auto ir = provider->createIR();
253

254
    // ir->addComposite(f);
255
256
257

    auto opt = xacc::getService<IRTransformation>("circuit-optimizer");

258
259
    opt->apply(f, nullptr);
    // auto optF = newir->getComposites()[0];
260

261
262
263
    // optF = optF->enabledView();
    CountGatesOfTypeVisitor<Rz> countRz(f);
    CountGatesOfTypeVisitor<Rx> countRx(f);
264

265
    EXPECT_EQ(3, f->nInstructions());
266
267
    EXPECT_EQ(1, countRz.countGates());
    EXPECT_EQ(0, countRx.countGates());
268
269
    EXPECT_TRUE(f->getInstruction(2)->getParameter(0).as<double>()==1.0 );
    std::cout << "FINAL CIRCUIT:\n" << f->toString() << "\n";
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

}

TEST(CircuitOptimizerTester, checkSimple) {

    auto c = xacc::getService<xacc::Compiler>("xasm");
    auto f = c->compile(R"(__qpu__ void foo1(qbit q) {
        CNOT(q[0],q[1]);
        CNOT(q[0],q[1]);
    })")
                 ->getComposites()[0];

  auto provider = xacc::getIRProvider("quantum");

    auto ir = provider->createIR();
    ir->addComposite(f);

    auto opt = xacc::getService<IRTransformation>("circuit-optimizer");
288
289
    opt->apply(f,nullptr);
    // auto optF = newir->getComposites()[0];
290

291
292
    // optF = optF->enabledView();
    EXPECT_EQ(0, f->nInstructions());
293
294
295
296
297
298
299
300
301

    f = c->compile(R"(__qpu__ void foo2(qbit q) {
        H(q[0]);
        CNOT(q[0],q[1]);
        CNOT(q[0],q[1]);
        H(q[0]);
    })")
            ->getComposites()[0];

302
303
    // ir = provider->createIR();
    // ir->addComposite(f);
304

305
306
307
    opt->apply(f, nullptr);
    // optF = newir->getComposites()[0];
    // optF = optF->enabledView();
308

309
    EXPECT_EQ(0, f->nInstructions());
310
311
312
313
314
315
316
317
318

    f = c->compile(R"(__qpu__ void foo3(qbit q) {
        CNOT(q[0],q[1]);
        H(q[1]);
        H(q[1]);
        CNOT(q[0],q[1]);
    })")
            ->getComposites()[0];

319
320
    // ir = provider->createIR();
    // ir->addComposite(f);
321

322
323
    opt->apply(f, nullptr);
    // optF = newir->getComposites()[0];
324

325
326
    // optF = f->enabledView();
    EXPECT_EQ(0, f->nInstructions());
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
TEST(CircuitOptimizerTester, checkPermuteAndCancelXGate) {
    // Case 1: back-to-back X gates
    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        auto program = compiler->compile(
            R"(__qpu__ void test1(qbit q) {
                X(q[0]);
                X(q[0]);
                X(q[0]);
                X(q[1]);
                X(q[0]);
                X(q[1]);
                X(q[1]);
            })")->getComposites()[0];
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        optimizer->apply(program, nullptr);
        EXPECT_EQ(1, program->nInstructions());
        // Only remain X(q[1]) instruction
        EXPECT_EQ("X", program->getInstruction(0)->name());
        EXPECT_EQ(1, program->getInstruction(0)->bits()[0]);
        std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
    }

    // Case 2: Permutation barriers
    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        auto program = compiler->compile(
            R"(__qpu__ void test2(qbit q) {
                X(q[0]);
                H(q[0]);
                X(q[0]);
                X(q[1]);
                CX(q[1], q[0]);
                X(q[1]);
                X(q[2]);
                Z(q[2]);
                X(q[2]);
            })")->getComposites()[0];
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        const auto nbInstBefore = program->nInstructions();
        optimizer->apply(program, nullptr);
        // No removal can be done
        EXPECT_EQ(nbInstBefore, program->nInstructions());
    }

    // Case 3: Permutation and cancel
    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        // 3 cases of permitted permutation: I gate (same qubit); CNOT (target qubit); arbitrary gates on different qubits.
        auto program = compiler->compile(
            R"(__qpu__ void test3(qbit q) {
                X(q[0]);
                I(q[0]);
                X(q[0]);
                X(q[1]);
                CX(q[0], q[1]);
                X(q[1]);
                X(q[2]);
                Z(q[0]);
                X(q[2]);
            })")->getComposites()[0];
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        optimizer->apply(program, nullptr);
        // We have removed all X gates.
        EXPECT_EQ(3, program->nInstructions());
        for (int i = 0; i < program->nInstructions(); ++i) {
            EXPECT_NE("X", program->getInstruction(i)->name());
        }
        std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
    }
}

TEST(CircuitOptimizerTester, checkHadamardGateReduction) {
    // Case 1: H-P-H
    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        auto program = compiler->compile(
            R"(__qpu__ void test4(qbit q) {
                H(q[0]);
                Rz(q[0], 1.57079632679489661923);
                H(q[0]);
            })")->getComposites()[0];
412

413
414
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        optimizer->apply(program, nullptr);
415

416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
        EXPECT_EQ(3, program->nInstructions());
        // Become P_dag - H - P_dag
        EXPECT_EQ("Rz", program->getInstruction(0)->name());
        EXPECT_EQ("H", program->getInstruction(1)->name());
        EXPECT_EQ("Rz", program->getInstruction(2)->name());
        EXPECT_NEAR(-M_PI_2, program->getInstruction(0)->getParameter(0).as<double>(), 1e-12);
        EXPECT_NEAR(-M_PI_2, program->getInstruction(2)->getParameter(0).as<double>(), 1e-12);
        std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
    }

    // Case 2: H-P_dag-H
    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        auto program = compiler->compile(
            R"(__qpu__ void test5(qbit q) {
                H(q[0]);
                Rz(q[0], -1.57079632679489661923);
                H(q[0]);
            })")->getComposites()[0];
435

436
437
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        optimizer->apply(program, nullptr);
438

439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
        EXPECT_EQ(3, program->nInstructions());
        // Become P - H - P
        EXPECT_EQ("Rz", program->getInstruction(0)->name());
        EXPECT_EQ("H", program->getInstruction(1)->name());
        EXPECT_EQ("Rz", program->getInstruction(2)->name());
        EXPECT_NEAR(M_PI_2, program->getInstruction(0)->getParameter(0).as<double>(), 1e-12);
        EXPECT_NEAR(M_PI_2, program->getInstruction(2)->getParameter(0).as<double>(), 1e-12);
        std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
    }

    // Case 3: H-H-CNOT-H-H
    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        auto program = compiler->compile(
            R"(__qpu__ void test6(qbit q) {
                H(q[0]);
                H(q[1]);
                CX(q[0], q[1]);
                H(q[1]);
                H(q[0]);
            })")->getComposites()[0];
460

461
462
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        optimizer->apply(program, nullptr);
463

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
        EXPECT_EQ(1, program->nInstructions());
        // Become CNOT q[1], q[0] (invert the control/target)
        EXPECT_EQ("CNOT", program->getInstruction(0)->name());
        EXPECT_EQ(1, program->getInstruction(0)->bits()[0]);
        EXPECT_EQ(0, program->getInstruction(0)->bits()[1]);
        std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
    }

    // Case 4: H-P-CNOT-P_dag-H
    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        auto program = compiler->compile(
            R"(__qpu__ void test7(qbit q) {
                H(q[0]);
                Rz(q[0], 1.57079632679489661923);
                CX(q[1], q[0]);
                Rz(q[0], -1.57079632679489661923);
                H(q[0]);
            })")->getComposites()[0];
483

484
485
486
487
488
489
490
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        optimizer->apply(program, nullptr);
        // Reduce two H gates
        EXPECT_EQ(3, program->nInstructions());
        // Become P_dag - CNOT - P
        EXPECT_EQ("Rz", program->getInstruction(0)->name());
        EXPECT_NEAR(-M_PI_2, program->getInstruction(0)->getParameter(0).as<double>(), 1e-12);
491

492
493
494
495
        // CNOT stays the same
        EXPECT_EQ("CNOT", program->getInstruction(1)->name());
        EXPECT_EQ(1, program->getInstruction(1)->bits()[0]);
        EXPECT_EQ(0, program->getInstruction(1)->bits()[1]);
496

497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
        EXPECT_EQ("Rz", program->getInstruction(2)->name());
        EXPECT_NEAR(M_PI_2, program->getInstruction(2)->getParameter(0).as<double>(), 1e-12);
        std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
    }

    // Case 5: H-P_dag-CNOT-P-H
    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        auto program = compiler->compile(
            R"(__qpu__ void test8(qbit q) {
                H(q[0]);
                Rz(q[0], -1.57079632679489661923);
                CX(q[1], q[0]);
                Rz(q[0], 1.57079632679489661923);
                H(q[0]);
            })")->getComposites()[0];
513

514
515
516
517
518
519
520
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        optimizer->apply(program, nullptr);
        // Reduce two H gates
        EXPECT_EQ(3, program->nInstructions());
        // Become P - CNOT - P_dag
        EXPECT_EQ("Rz", program->getInstruction(0)->name());
        EXPECT_NEAR(M_PI_2, program->getInstruction(0)->getParameter(0).as<double>(), 1e-12);
521

522
523
524
525
        // CNOT stays the same
        EXPECT_EQ("CNOT", program->getInstruction(1)->name());
        EXPECT_EQ(1, program->getInstruction(1)->bits()[0]);
        EXPECT_EQ(0, program->getInstruction(1)->bits()[1]);
526

527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
        EXPECT_EQ("Rz", program->getInstruction(2)->name());
        EXPECT_NEAR(-M_PI_2, program->getInstruction(2)->getParameter(0).as<double>(), 1e-12);
        std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
    }

    // Case 6: H-P_dag-CNOT^k-P-H : multiple CNOT with the same target qubit
    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        auto program = compiler->compile(
            R"(__qpu__ void test9(qbit q) {
                H(q[0]);
                Rz(q[0], -1.57079632679489661923);
                CX(q[1], q[0]);
                CX(q[2], q[0]);
                CX(q[3], q[0]);
                CX(q[4], q[0]);
                CX(q[5], q[0]);
                CX(q[6], q[0]);
                Rz(q[0], 1.57079632679489661923);
                H(q[0]);
            })")->getComposites()[0];
548

549
550
551
552
553
554
555
556
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        const auto nbInstructionsBefore = program->nInstructions();
        optimizer->apply(program, nullptr);
        // Reduce two H gates
        EXPECT_EQ(nbInstructionsBefore - 2, program->nInstructions());
        // Become P - CNOT^k - P_dag
        EXPECT_EQ("Rz", program->getInstruction(0)->name());
        EXPECT_NEAR(M_PI_2, program->getInstruction(0)->getParameter(0).as<double>(), 1e-12);
557

558
559
560
561
562
        // CNOT's stays the same
        for (int i = 1; i < program->nInstructions() - 1; ++i) {
            EXPECT_EQ("CNOT", program->getInstruction(i)->name());
            EXPECT_EQ(0, program->getInstruction(i)->bits()[1]);
        }
563
564

        // Last gate is P_dag
565
566
567
568
569
570
571
572
        EXPECT_EQ("Rz", program->getInstruction(program->nInstructions() - 1)->name());
        EXPECT_NEAR(-M_PI_2, program->getInstruction(program->nInstructions() - 1)->getParameter(0).as<double>(), 1e-12);
        std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
    }

    // TODO: Add more complex test cases, e.g. combinations of multiple patterns.
}

573
574
575
576
577
578
579
580
581
582
583
TEST(CircuitOptimizerTester, checkRotationMergingUsingPhasePolynomials) {
    const auto countRzGates = [](const std::shared_ptr<CompositeInstruction>& in_program){
        int count = 0;
        for (int i = 0; i < in_program->nInstructions(); ++i) {
            const auto& inst = in_program->getInstruction(i);
            if (inst->name() == "Rz") {
                ++count;
            }
        }
        return count;
    };
584

585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        // Simple test: circuit (7) in page 13 of https://arxiv.org/pdf/1710.07345.pdf
        auto program = compiler->compile(
            R"(__qpu__ void test10(qbit q) {
                H(q[0]);
                Rz(q[1], 1.0);
                CNOT(q[0], q[1]);
                Rz(q[1], 2.0);
                CNOT(q[0], q[1]);
                Rz(q[0], 3.0);
                Rz(q[1], 4.0);
                CNOT(q[1], q[0]);
                X(q[1]);
                H(q[1]);
                H(q[0]);
            })")->getComposites()[0];
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
        const auto gateCountBefore = program->nInstructions();
        const auto rzCountBefore = countRzGates(program);
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        optimizer->apply(program, nullptr);
        // We remove one Rz gate
        EXPECT_EQ(program->nInstructions(), gateCountBefore -1);
        EXPECT_EQ(countRzGates(program), rzCountBefore - 1);
        std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
    }

    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        // Test: circuit (8) in page 14 of https://arxiv.org/pdf/1710.07345.pdf
        auto program = compiler->compile(
            R"(__qpu__ void test11(qbit q) {
                H(q[0]);
                H(q[1]);
                H(q[2]);
                Rz(q[1], 1.0);
                Rz(q[2], 2.0);
                CNOT(q[1], q[0]);
                CNOT(q[1], q[2]);
                Rz(q[0], 3.0);
                CNOT(q[0], q[1]);
                H(q[2]);
                CNOT(q[1], q[2]);
                CNOT(q[0], q[1]);
                Rz(q[1], 4.0);
                H(q[1]);
                H(q[0]);
635
            })")->getComposites()[0];
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
        const auto gateCountBefore = program->nInstructions();
        const auto rzCountBefore = countRzGates(program);
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        optimizer->apply(program, nullptr);
        // We remove one Rz gate (last one in the figure)
        EXPECT_EQ(program->nInstructions(), gateCountBefore -1);
        EXPECT_EQ(countRzGates(program), rzCountBefore - 1);
        std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
    }

    {
        auto compiler = xacc::getService<xacc::Compiler>("xasm");
        // Test circuit pruning: similar to circuit (8) in page 14 of https://arxiv.org/pdf/1710.07345.pdf,
        // but we swap (ctrl, target) qubits of CNOT#4 => it must correctly identify it as a termination point.
        auto program = compiler->compile(
            R"(__qpu__ void test12(qbit q) {
                H(q[0]);
                H(q[1]);
                H(q[2]);
                Rz(q[1], 1.0);
                Rz(q[2], 2.0);
                CNOT(q[1], q[0]);
                CNOT(q[1], q[2]);
                Rz(q[0], 3.0);
                CNOT(q[0], q[1]);
                H(q[2]);
                CNOT(q[2], q[1]);
                CNOT(q[0], q[1]);
                Rz(q[1], 4.0);
                H(q[1]);
                H(q[0]);
667
            })")->getComposites()[0];
668
669
670
671
672
673
674
675
676
677
678
679
        const auto gateCountBefore = program->nInstructions();
        const auto rzCountBefore = countRzGates(program);
        auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
        optimizer->apply(program, nullptr);
        // We cannot remove any Rz gates in this case:
        // the last Rz (which can be merged in the previous case) is now outside the subcircuit.
        EXPECT_EQ(program->nInstructions(), gateCountBefore);
        EXPECT_EQ(countRzGates(program), rzCountBefore);
        std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
    }
}

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
TEST(CircuitOptimizerTester, checkCCCX) {
  auto compiler = xacc::getService<xacc::Compiler>("staq");
  auto program = compiler
                     ->compile(
                         R"#(
OPENQASM 2.0;
include "qelib1.inc";
qreg q[4];
creg c[4];
rz(pi/2) q[3];
ry(pi/4) q[3];
cx q[0],q[3];
ry(-pi/4) q[3];
cx q[0],q[3];
rz(pi/2) q[0];
ry(pi/4) q[0];
rz(-pi/2) q[3];
rz(pi/2) q[3];
ry(pi/8) q[3];
cx q[1],q[3];
ry(-pi/8) q[3];
cx q[1],q[3];
cx q[1],q[0];
ry(-pi/4) q[0];
cx q[1],q[0];
rz(-pi/2) q[0];
rz(pi/2) q[0];
ry(pi/4) q[0];
rz(pi/2) q[1];
ry(pi/2) q[1];
rz(-pi/2) q[3];
rz(pi/2) q[3];
ry(pi/8) q[3];
cx q[2],q[3];
ry(-pi/8) q[3];
cx q[2],q[3];
cx q[2],q[0];
ry(-pi/4) q[0];
cx q[2],q[0];
rz(-pi/2) q[0];
rz(pi/2) q[0];
cx q[2],q[1];
ry(-pi/2) q[1];
cx q[2],q[1];
rz(-pi/2) q[1];
rz(-pi/2) q[3];
rz(pi/2) q[3];
cx q[1],q[3];
ry(pi/8) q[3];
cx q[1],q[3];
cx q[1],q[0];
ry(pi/4) q[0];
cx q[1],q[0];
ry(-pi/4) q[0];
rz(-pi/2) q[0];
ry(-pi/8) q[3];
rz(-pi/2) q[3];
rz(pi/2) q[3];
cx q[0],q[3];
ry(pi/4) q[3];
cx q[0],q[3];
rz(pi/2) q[0];
ry(pi/4) q[0];
cx q[1],q[0];
ry(-pi/4) q[0];
cx q[1],q[0];
rz(-pi/2) q[0];
rz(pi/2) q[0];
rz(pi/2) q[1];
cx q[2],q[1];
ry(pi/2) q[1];
cx q[2],q[1];
ry(-pi/2) q[1];
rz(-pi/2) q[1];
cx q[2],q[0];
ry(pi/4) q[0];
cx q[2],q[0];
ry(-pi/4) q[0];
rz(-pi/2) q[0];
rz(pi/2) q[0];
cx q[1],q[0];
ry(pi/4) q[0];
cx q[1],q[0];
ry(-pi/4) q[0];
rz(-pi/2) q[0];
ry(-pi/4) q[3];
rz(-pi/2) q[3];
measure q[0] -> c[0];
measure q[1] -> c[1];
measure q[2] -> c[2];
measure q[3] -> c[3];
)#")
                     ->getComposites()[0];
  std::cout << "HOWDY CIRCUIT:\n" << program->toString() << "\n";

  // Evaluate all possible inputs.
  const auto getTruthTable = [](auto program) {
    std::vector<std::string> results;
    // 4 bits: run all test cases
    for (int dec_val = 0; dec_val < (1 << 4); ++dec_val) {
      auto provider = xacc::getService<IRProvider>("quantum");
      auto test_program =
          provider->createComposite("test" + std::to_string(dec_val));

      for (size_t bitIdx = 0; bitIdx < 4; ++bitIdx) {
        if ((dec_val & (1 << bitIdx)) == (1 << bitIdx)) {
          test_program->addInstruction(
              provider->createInstruction("X", bitIdx));
        }
      }

      test_program->addInstructions(program->getInstructions());

      auto accelerator = xacc::getAccelerator("qpp", {{"shots", 1024}});
      auto buffer = xacc::qalloc(4);
      accelerator->execute(buffer, test_program);
      std::cout << "Test case: " << dec_val << "\n";
      buffer->print();
      EXPECT_EQ(buffer->getMeasurements().size(), 1);
      results.emplace_back(buffer->getMeasurements()[0]);
    }
    return results;
  };

  const auto truthTableBefore = getTruthTable(program);
  EXPECT_EQ(truthTableBefore.size(), 16);

  // Apply optimization
  auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
  const auto nBefore = program->nInstructions();
  optimizer->apply(program, nullptr);
  const auto nAfter = program->nInstructions();
  // Reduce instructions
  EXPECT_TRUE(nAfter < nBefore);
  std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";

  const auto truthTableAfter = getTruthTable(program);
  EXPECT_EQ(truthTableAfter.size(), 16);
  EXPECT_EQ(truthTableBefore, truthTableAfter);
}

821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
TEST(CircuitOptimizerTester, checkCZ) {
  xacc::set_verbose(true);
  auto compiler = xacc::getService<xacc::Compiler>("xasm");
  auto program = compiler
                     ->compile(R"(__qpu__ void testCz(qbit q) {
        CNOT(q[0], q[1]);
        CZ(q[0], q[1]);
        Measure(q[0]);
        Measure(q[1]);
    })")
                     ->getComposites()[0];

  const auto before_xasm_str = program->toString();
  auto optimizer = xacc::getService<IRTransformation>("circuit-optimizer");
  optimizer->apply(program, nullptr);
  std::cout << "FINAL CIRCUIT:\n" << program->toString() << "\n";
  const auto after_xasm_str = program->toString();
  EXPECT_EQ(before_xasm_str, after_xasm_str);
}

841
842
843
844
845
846
847
int main(int argc, char **argv) {
  xacc::Initialize(argc, argv);
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  xacc::Finalize();
  return ret;
}