Skip to content

Commit

Permalink
Simplify QFT code in phase estimation benchmark, and add init_phase o…
Browse files Browse the repository at this point in the history
…ption
  • Loading branch information
rtvuser1 committed Jun 9, 2024
1 parent 63708e2 commit 4bbbb50
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
18 changes: 14 additions & 4 deletions phase-estimation/qiskit/pe_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def theta_to_bitstring(theta, num_counting_qubits):
# Convert bitstring to theta representation, useful for debugging
def bitstring_to_theta(counts, num_counting_qubits):
theta_counts = {}
for key in counts.keys():
r = counts[key]
for item in counts.items():
key, r = item
theta = int(key,2) / (2**num_counting_qubits)
if theta not in theta_counts.keys():
theta_counts[theta] = 0
Expand All @@ -85,6 +85,7 @@ def bitstring_to_theta(counts, num_counting_qubits):

# Execute program with default parameters
def run(min_qubits=3, max_qubits=8, skip_qubits=1, max_circuits=3, num_shots=100,
init_phase=None,
backend_id=None, provider_backend=None,
hub="ibm-q", group="open", project="main", exec_options=None,
context=None):
Expand Down Expand Up @@ -149,6 +150,11 @@ def execution_handler(qc, result, num_qubits, theta, num_shots):

# loop over limited # of random theta choices
for theta in theta_range:
theta = float(theta)

# if initial phase passed in, use it instead of random values
if init_phase:
theta = init_phase

# create the circuit for given qubit size and theta, store time metric
ts = time.time()
Expand Down Expand Up @@ -189,7 +195,8 @@ def get_args():
parser.add_argument("--skip_qubits", "-k", default=1, help="Number of qubits to skip", type=int)
parser.add_argument("--max_circuits", "-c", default=3, help="Maximum circuit repetitions", type=int)
parser.add_argument("--method", "-m", default=1, help="Algorithm Method", type=int)
parser.add_argument("--theta", default=0.0, help="Input Theta Value", type=float)
parser.add_argument("--init_phase", "-p", default=0.0, help="Input Phase Value", type=float)
parser.add_argument("--nonoise", "-non", action="store_true", help="Use Noiseless Simulator")
parser.add_argument("--verbose", "-v", action="store_true", help="Verbose")
return parser.parse_args()

Expand All @@ -203,15 +210,18 @@ def get_args():

# special argument handling
ex.verbose = args.verbose
verbose = args.verbose

if args.num_qubits > 0: args.min_qubits = args.max_qubits = args.num_qubits

# execute benchmark program
run(min_qubits=args.min_qubits, max_qubits=args.max_qubits,
skip_qubits=args.skip_qubits, max_circuits=args.max_circuits,
num_shots=args.num_shots,
#method=args.method,
#theta=args.theta,
init_phase=args.init_phase,
backend_id=args.backend_id,
exec_options = {"noise_model" : None} if args.nonoise else {},
#api=args.api
)

Expand Down
22 changes: 20 additions & 2 deletions phase-estimation/qiskit/pe_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ def inv_qft_gate(input_size):
#global QFTI_, num_gates, depth
global QFTI_
qr = QuantumRegister(input_size); qc = QuantumCircuit(qr, name="inv_qft")

'''
# Generate multiple groups of diminishing angle CRZs and H gate
for i_qubit in reversed(range(0, input_size)):

# start laying out gates from highest order qubit (the hidx)
hidx = input_size - i_qubit - 1
Expand All @@ -101,6 +100,25 @@ def inv_qft_gate(input_size):
qc.barrier()
qc.barrier()
'''
# Generate multiple groups of diminishing angle CRZs and H gate
for i_qubit in range(input_size):

# precede with an H gate (applied to all qubits)
qc.h(qr[i_qubit])

# number of controlled Z rotations to perform at this level
num_crzs = input_size - i_qubit - 1

# if not the highest order qubit, add multiple controlled RZs of decreasing angle
if i_qubit < input_size - 1:
for j in range(0, num_crzs):
divisor = 2 ** (j + 1)
qc.crz( -math.pi / divisor , qr[i_qubit], qr[i_qubit + j + 1])

qc.barrier()

if QFTI_ == None or input_size <= 5:
if input_size < 9: QFTI_= qc

Expand Down

0 comments on commit 4bbbb50

Please sign in to comment.