Computation speed benchmark
Scope
This notebook is an environment-specific engineering benchmark. It helps estimate how statevector dimension affects the current local backend. Absolute timings should be compared only when the recorded machine and software environment are similar.
The benchmark measures mean execution time for several model operations and dimensions under the recorded environment.
import multiprocessing
import os
import platform
import re
import subprocess
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from IPython.display import HTML, display
Local machine details
Log the local machine details on which the test is carried out:
def get_cpu_model(spec="model name"):
if platform.system() == "Windows":
return platform.processor()
if platform.system() == "Darwin":
try:
return subprocess.check_output(
["sysctl", "-n", "machdep.cpu.brand_string"], text=True
).strip()
except OSError, subprocess.CalledProcessError:
return platform.processor() or platform.machine()
if platform.system() == "Linux":
command = "cat /proc/cpuinfo"
stream = os.popen(command)
all_info = stream.read()
all_info.strip()
all_info = all_info.split("\n")
# all_info = str(subprocess.check_output(command, shell=True).strip())
for line in all_info:
if spec in line:
return re.sub(f".*{spec}.*: ", "", line, 1)
def get_specs():
table = [
# ["", platform.version()],
["Machine", platform.machine()],
["Platform", platform.platform()],
["Architecture", platform.architecture()],
["Cores", get_cpu_model("model name")],
["Number of cores", multiprocessing.cpu_count()],
["Python version", platform.python_version()],
]
dataframe = pd.DataFrame(table, columns=["Spec", "Value"])
display(HTML(dataframe.to_html(index=False)))
get_specs()
/tmp/ipykernel_1123/3135866240.py:20: DeprecationWarning: 'count' is passed as positional argument
return re.sub(f".*{spec}.*: ", "", line, 1)
| Spec | Value |
|---|---|
| Machine | x86_64 |
| Platform | Linux-7.0.0-1004-aws-x86_64-with-glibc2.35 |
| Architecture | (64bit, ) |
| Cores | AMD EPYC 7R13 Processor |
| Number of cores | 2 |
| Python version | 3.14.6 |
Test function
Create a test function to test the models:
def test_model(Model, n):
init_time = list()
encode_time = list()
decode_time = list()
total_time = list()
# Iterations for computing mean and standard deviation
iterations = 50
for i in range(1, iterations + 1):
# Initialization
start_process = time.time()
model = Model(n, 1)
# Encoding
start_encode = time.time()
for dim in range(model.n):
model.encode(0.5, dim)
# Decoding
start_decode = time.time()
result = model.decode()
end_process = time.time()
# Store timings
init_time.append(start_encode - start_process)
encode_time.append(start_decode - start_encode)
decode_time.append(end_process - start_decode)
total_time.append(end_process - start_process)
return [
n,
np.mean(init_time),
np.mean(encode_time),
np.mean(decode_time),
np.mean(total_time),
np.std(total_time),
]
def plot_results(df, title="Model"):
"""Plot all the results"""
# Total timewith STD
fig = plt.figure(figsize=(15, 6), dpi=150)
df.plot(
x="n",
y=[4],
yerr="Total STD",
uplims=True,
lolims=True,
kind="line",
color="r",
ax=plt.gca(),
)
plt.title(f"{title} - Total time with STD")
plt.grid(visible=True, which="major", linestyle="-")
plt.grid(visible=True, which="minor", linestyle="--", alpha=0.2)
plt.minorticks_on()
plt.show()
# Timings insight
fig = plt.figure(figsize=(15, 6), dpi=150)
df.plot(x="n", y=[1, 2, 3, 4], kind="line", ax=plt.gca())
plt.title(f"{title} - Timings insight")
plt.grid(visible=True, which="major", linestyle="-")
plt.grid(visible=True, which="minor", linestyle="--", alpha=0.2)
plt.minorticks_on()
plt.show()
# Zooming in
print("Zooming in:")
fig = plt.figure(figsize=(15, 6), dpi=150)
df.plot(x="n", y=[1, 2], kind="line", ax=plt.subplot(1, 2, 1))
plt.title("Initialization and encoding only")
plt.grid(visible=True, which="major", linestyle="-")
plt.grid(visible=True, which="minor", linestyle="--", alpha=0.2)
plt.minorticks_on()
ax = plt.subplot(1, 2, 2)
df[1:19].plot(x="n", y=[1, 2, 3], kind="line", ax=ax)
df[1:19].plot(x="n", y=[4], yerr="Total STD", kind="line", ax=ax)
plt.title("Showing results for n < 20")
plt.grid(visible=True, which="major", linestyle="-")
plt.grid(visible=True, which="minor", linestyle="--", alpha=0.2)
plt.minorticks_on()
plt.show()
AngularModel
We test initialization, encoding, and decoding for an input of .5 on each dimension. The executable documentation caps the statevector benchmark at 12 qubits so it remains practical on documentation builders.
from qrobot.models import AngularModel
max_n = 12
table = list()
for n in range(1, max_n + 1):
print(f"Testing n={n}", end="\r")
table.append(test_model(AngularModel, n))
print(" ")
df_angular = pd.DataFrame(
table, columns=["n", "Initialization", "Encode", "Decode", "Total", "Total STD"]
)
Testing n=1
Testing n=2
Testing n=3
Testing n=4
Testing n=5
Testing n=6
Testing n=7
Testing n=8
Testing n=9
Testing n=10
Testing n=11
Testing n=12
Plotting the results:
plot_results(df_angular, "AngularModel")
Zooming in:
Numerical values:
df_angular
| n | Initialization | Encode | Decode | Total | Total STD | |
|---|---|---|---|---|---|---|
| 0 | 1 | 0.000035 | 0.000018 | 0.000416 | 0.000469 | 0.000085 |
| 1 | 2 | 0.000032 | 0.000025 | 0.000466 | 0.000523 | 0.000489 |
| 2 | 3 | 0.000033 | 0.000033 | 0.000460 | 0.000525 | 0.000018 |
| 3 | 4 | 0.000032 | 0.000039 | 0.000537 | 0.000609 | 0.000105 |
| 4 | 5 | 0.000034 | 0.000046 | 0.000619 | 0.000700 | 0.000029 |
| 5 | 6 | 0.000034 | 0.000055 | 0.000748 | 0.000837 | 0.000115 |
| 6 | 7 | 0.000035 | 0.000061 | 0.001007 | 0.001103 | 0.000146 |
| 7 | 8 | 0.000038 | 0.000070 | 0.001738 | 0.001846 | 0.000138 |
| 8 | 9 | 0.000037 | 0.000078 | 0.002213 | 0.002328 | 0.000295 |
| 9 | 10 | 0.000039 | 0.000084 | 0.003968 | 0.004091 | 0.000292 |
| 10 | 11 | 0.000075 | 0.000116 | 0.011781 | 0.011972 | 0.000374 |
| 11 | 12 | 0.000086 | 0.000118 | 0.025043 | 0.025248 | 0.000831 |
LinearModel
We test initialization, encoding, and decoding for an input of .5 on each dimension up to the same 12-qubit practical documentation limit.
from qrobot.models import LinearModel
max_n = 12
table = list()
for n in range(1, max_n + 1):
print(f"Testing n={n}", end="\r")
table.append(test_model(LinearModel, n))
print(" ")
df_linear = pd.DataFrame(
table, columns=["n", "Initialization", "Encode", "Decode", "Total", "Total STD"]
)
Testing n=1
Testing n=2
Testing n=3
Testing n=4
Testing n=5
Testing n=6
Testing n=7
Testing n=8
Testing n=9
Testing n=10
Testing n=11
Testing n=12
plot_results(df_linear, "LinearModel")
Zooming in:
df_linear
| n | Initialization | Encode | Decode | Total | Total STD | |
|---|---|---|---|---|---|---|
| 0 | 1 | 0.000032 | 0.000022 | 0.000401 | 0.000455 | 0.000116 |
| 1 | 2 | 0.000032 | 0.000028 | 0.000517 | 0.000577 | 0.000037 |
| 2 | 3 | 0.000032 | 0.000036 | 0.000616 | 0.000683 | 0.000102 |
| 3 | 4 | 0.000032 | 0.000043 | 0.000714 | 0.000789 | 0.000028 |
| 4 | 5 | 0.000033 | 0.000050 | 0.000833 | 0.000916 | 0.000112 |
| 5 | 6 | 0.000032 | 0.000057 | 0.000968 | 0.001057 | 0.000020 |
| 6 | 7 | 0.000034 | 0.000065 | 0.001187 | 0.001286 | 0.000126 |
| 7 | 8 | 0.000037 | 0.000071 | 0.001563 | 0.001672 | 0.000047 |
| 8 | 9 | 0.000034 | 0.000075 | 0.001924 | 0.002033 | 0.000252 |
| 9 | 10 | 0.000039 | 0.000083 | 0.003310 | 0.003432 | 0.000143 |
| 10 | 11 | 0.000055 | 0.000103 | 0.006597 | 0.006756 | 0.000200 |
| 11 | 12 | 0.000082 | 0.000121 | 0.014254 | 0.014458 | 0.000344 |
Comparison
fig = plt.figure(figsize=(15, 6), dpi=150)
df_angular.plot(x="n", y=[4], kind="line", ax=plt.gca())
df_linear.plot(x="n", y=[4], kind="line", ax=plt.gca())
plt.legend(["AngularModel", "LinearModel"])
plt.title("Total times comparison")
plt.grid(visible=True, which="major", linestyle="-")
plt.grid(visible=True, which="minor", linestyle="--", alpha=0.2)
plt.minorticks_on()
plt.show()
fig = plt.figure(figsize=(15, 6), dpi=150)
df_angular.plot(x="n", y=[5], kind="line", ax=plt.gca())
df_linear.plot(x="n", y=[5], kind="line", ax=plt.gca())
plt.legend(["AngularModel", "LinearModel"])
plt.title("Total times STD comparison")
plt.grid(visible=True, which="major", linestyle="-")
plt.grid(visible=True, which="minor", linestyle="--", alpha=0.2)
plt.minorticks_on()
plt.show()
fig = plt.figure(figsize=(15, 6), dpi=150)
df_angular[1:19].plot(x="n", y=[4], kind="line", ax=plt.gca())
df_linear[1:19].plot(x="n", y=[4], kind="line", ax=plt.gca())
plt.legend(["AngularModel", "LinearModel"])
plt.title("Total times comparison (n < 20)")
plt.grid(visible=True, which="major", linestyle="-")
plt.grid(visible=True, which="minor", linestyle="--", alpha=0.2)
plt.minorticks_on()
plt.show()