Skip to content

Instantly share code, notes, and snippets.

@Scinawa
Created December 20, 2017 01:14
Show Gist options
  • Save Scinawa/2e262c3c0ca51fd68816f951eeb90175 to your computer and use it in GitHub Desktop.
Save Scinawa/2e262c3c0ca51fd68816f951eeb90175 to your computer and use it in GitHub Desktop.
Space estimation for HHL
#!/usr/bin/python3
import matplotlib.pyplot as plt
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--nqubit", required=False, help="number of qubits", type=int)
parser.add_argument("--precision", required=False, default=32, type=int)
parser.add_argument("--graph", help="plot a graph!", action='store_true', default=False)
qubits = list(range(34,200))
precisions = [32,64]
sparsity = [1,100]
def space_resource_estimation (qubit, precision ):
"""
- 1 ancilla qubit :)
- k qubit of precision for phase estimation
- rest for storing the state $b$ and get $x$.
classically, we have to save a vector of equal length
using 64bits of precision (we don't have anymore 32bits CPUs)
"""
# ancilla
usable = qubit - 1
# phase estimation space
storage_qubits = usable - precision
# qubits for I/O
vector_dimension = 2**storage_qubits
# how many bytes for a vector of this dimension with this precision? / convert in tera
terabytes = (vector_dimension * precision / (1.25*(10**13)))
#print("qubit {} precision {} terabytes {:f}".format(qubit, precision, terabytes))
return terabytes
def generate_plot():
"""
We generate the plot
"""
fig = plt.figure()
ax = fig.add_subplot(111)
plt.title("Qubit vs Space")
for s in sparsity:
for precision in precisions:
space = list(map(lambda x : space_resource_estimation(x, precision)*s, qubits))
ax.plot(qubits, space, label='Precision: {0} - Sparsity = {1}'.format(precision, s))
plt.axhline(y=400000, color='grey', linewidth=1, linestyle='-')
plt.ylabel('Classical storage (TB)')
plt.xlabel('Qubits')
plt.yscale("log")
plt.legend(loc='upper left')
plt.legend(shadow=True, fancybox=True)
plt.savefig('space_resource_estimation.png')
if __name__ == '__main__':
args = parser.parse_args()
if args.nqubit:
for precision in precisions:
tb = space_resource_estimation(args.nqubit, precision)
print("With {0} qubits and {1} bits of precision we need {2:f} TB of space ".format(args.nqubit, precision, tb,
sparsity))
sys.exit()
if args.graph:
generate_plot()
sys.exit()
parser.parse_args(['-h'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment