QNLP  v1.0
ncu_opt_tester.py
Go to the documentation of this file.
1 """
2 Shows the performance difference in optimised versus non optimised NCU operations.
3 
4 mpirun -n 8 python ./ncu_opt_tester.py <num ctrl lines> <operating mode: {0,1}>
5 
6 mpirun -n 8 python ./ncu_opt_tester.py 11 0 # Should be realtively fast
7 mpirun -n 8 python ./ncu_opt_tester.py 11 1 # Should take a while
8 
9 """
10 
11 from PyQNLPSimulator import PyQNLPSimulator as p
12 import sys
13 from mpi4py import MPI
14 
15 comm = MPI.COMM_WORLD
16 rank = comm.Get_rank()
17 
18 num_ctrl = int(sys.argv[1])
19 num_qubits = num_ctrl*2 - 1
20 
21 target = num_qubits-1
22 
23 sim = p(num_qubits, False)
24 regD = range(num_ctrl)
25 regA = range(num_ctrl, target)
26 
27 sim.initRegister()
28 
29 for i in regD:
30  if rank == 0:
31  print("Applying gateX to {}".format(i))
32  sim.applyGateX(i)
33 
34 res0 = sim.applyMeasurementToRegister([target], True)
35 
36 if sys.argv[2] == "1": #unoptimised
37  sim.applyGateNCU(sim.getGateX(), regD, target ,"X")
38 else: #optimised
39  sim.applyGateNCU(sim.getGateX(), regD, regA, target ,"X")
40 
41 res1 = sim.applyMeasurementToRegister([target], True)
42 
43 if rank == 0:
44  print(list(regD) + [target], list(regA), "Before NCU={}".format(res0), "After NCU={}".format(res1))