在 amazon-braket 上的大型矩阵上应用 QPE
Posted
技术标签:
【中文标题】在 amazon-braket 上的大型矩阵上应用 QPE【英文标题】:Applying QPE on a large matrix on amazon-braket 【发布时间】:2022-01-17 10:20:09 【问题描述】:我在 amazon-braket 上运行 QPE 算法,但它只能应用于 22 或 44 矩阵,当我想将其扩展为 5*5 或更大时,它会出现错误。据我所知,QPE可以解决的矩阵大小没有理论上的限制。
这里是代码的主要部分:
def get_qpe_phases(measurement_counts, precision_qubits, items_to_keep=1):
bitstrings_precision_register = [
substring(key, precision_qubits) for key in measurement_counts.keys()
]
bitstrings_precision_register_set = set(bitstrings_precision_register)
bitstrings_precision_register_list = list(bitstrings_precision_register_set)
precision_results_dic = key: 0 for key in bitstrings_precision_register_list
for key in measurement_counts.keys():
counts = measurement_counts[key]
count_key = substring(key, precision_qubits)
precision_results_dic[count_key] += counts
c = Counter(precision_results_dic)
topmost = c.most_common(items_to_keep)
phases_decimal = [binaryToDecimal(item[0]) for item in topmost]
return phases_decimal, precision_results_dic
def run_qpe(
unitary,
precision_qubits,
query_qubits,
query_circuit,
items_to_keep=1,
shots=1000
):
circ = query_circuit
circ.qpe(precision_qubits, query_qubits, unitary, control_unitary=False)
# Add desired results_types
number_precision_qubits = len(precision_qubits)
num_qubits = len(precision_qubits)
circ.probability(np.array(precision_qubits))
device = LocalSimulator()
task=device.run(circ,shots=shots)
result=task.result()
metadata = result.task_metadata
probs_values = result.values[0]
measurements = result.measurements
measured_qubits = result.measured_qubits
measurement_counts = result.measurement_counts
measurement_probabilities = result.measurement_probabilities
format_bitstring = "0:0" + str(num_qubits) + "b"
bitstring_keys = [format_bitstring.format(ii) for ii in range(2 ** num_qubits)]
phases_decimal, precision_results_dic = get_qpe_phases(
measurement_counts, precision_qubits, items_to_keep
)
print('\nPhases:',phases_decimal)
eigenvalues = [np.exp(2 * np.pi * 1j * phase) for phase in phases_decimal]
# aggregate results
out =
"circuit": circ,
"task_metadata": metadata,
"measurements": measurements,
"measured_qubits": measured_qubits,
"measurement_counts": measurement_counts,
"measurement_probabilities": measurement_probabilities,
"probs_values": probs_values,
"bitstring_keys": bitstring_keys,
"precision_results_dic": precision_results_dic,
"phases_decimal": phases_decimal,
"eigenvalues": eigenvalues,
return out
当涉及到 6*6 矩阵时,
from scipy.stats import unitary_group
import numpy as np
np.random.seed(seed=3000)
unitary1 = unitary_group.rvs(6)
result = estimate_phase(4,unitary1,5,printcir=False)
错误报告是这样的:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-21-4eae0beac402> in <module>
4
5 unitary = unitary_group.rvs(6)
----> 6 result = estimate_phase(4,unitary,5,printcir=False)
<ipython-input-9-30dffae9e6c6> in estimate_phase(n, unitary, runit, shots, printcir)
15 query = Circuit().h(query_qubits)
16
---> 17 result = run_qpe(unitary, precision_qubits, query_qubits, query, items_to_keep=elen, shots = shots)
18 qeigvals= postprocess_qpe_results(result,printcir)
19
<ipython-input-6-1875e0e5a610> in run_qpe(unitary, precision_qubits, query_qubits, query_circuit, items_to_keep, shots)
92 ):
93 circ = query_circuit
---> 94 circ.qpe(precision_qubits, query_qubits, unitary, control_unitary=False)
95
96 # Add desired results_types
~/anaconda3/envs/Braket/lib/python3.7/site-packages/braket/circuits/circuit.py in method_from_subroutine(self, *args, **kwargs)
75
76 def method_from_subroutine(self, *args, **kwargs) -> SubroutineReturn:
---> 77 return self.add(func, *args, **kwargs)
78
79 function_name = func.__name__
~/anaconda3/envs/Braket/lib/python3.7/site-packages/braket/circuits/circuit.py in add(self, addable, *args, **kwargs)
495 self.add_circuit(item, *args, **kwargs)
496 elif callable(item):
--> 497 self.add(item(*args, **kwargs))
498 else:
499 raise TypeError(f"Cannot add a 'type(item)' to a Circuit")
<ipython-input-6-1875e0e5a610> in qpe(precision_qubits, query_qubits, unitary, control_unitary)
39 else:
40 for _ in range(2 ** power):
---> 41 qpe_circ.controlled_unitary(qubit, query_qubits, unitary)
42 qpe_circ.inverse_qft(precision_qubits)
43
~/anaconda3/envs/Braket/lib/python3.7/site-packages/braket/circuits/circuit.py in method_from_subroutine(self, *args, **kwargs)
75
76 def method_from_subroutine(self, *args, **kwargs) -> SubroutineReturn:
---> 77 return self.add(func, *args, **kwargs)
78
79 function_name = func.__name__
~/anaconda3/envs/Braket/lib/python3.7/site-packages/braket/circuits/circuit.py in add(self, addable, *args, **kwargs)
495 self.add_circuit(item, *args, **kwargs)
496 elif callable(item):
--> 497 self.add(item(*args, **kwargs))
498 else:
499 raise TypeError(f"Cannot add a 'type(item)' to a Circuit")
<ipython-input-6-1875e0e5a610> in controlled_unitary(control, target_qubits, unitary)
21 targets = [control] + target_qubits
22
---> 23 circ.unitary(matrix=controlled_matrix, targets=targets)
24
25 return circ
~/anaconda3/envs/Braket/lib/python3.7/site-packages/braket/circuits/circuit.py in method_from_subroutine(self, *args, **kwargs)
75
76 def method_from_subroutine(self, *args, **kwargs) -> SubroutineReturn:
---> 77 return self.add(func, *args, **kwargs)
78
79 function_name = func.__name__
~/anaconda3/envs/Braket/lib/python3.7/site-packages/braket/circuits/circuit.py in add(self, addable, *args, **kwargs)
495 self.add_circuit(item, *args, **kwargs)
496 elif callable(item):
--> 497 self.add(item(*args, **kwargs))
498 else:
499 raise TypeError(f"Cannot add a 'type(item)' to a Circuit")
~/anaconda3/envs/Braket/lib/python3.7/site-packages/braket/circuits/gates.py in unitary(targets, matrix, display_name)
1339 """
1340 if 2 ** len(targets) != matrix.shape[0]:
-> 1341 raise ValueError("Dimensions of the supplied unitary are incompatible with the targets")
1342
1343 return Instruction(Gate.Unitary(matrix, display_name), target=targets)
ValueError: Dimensions of the supplied unitary are incompatible with the targets
所以我想知道是包的问题还是程序的问题或任何其他遇到了与我类似的问题?
提前致谢!
【问题讨论】:
【参考方案1】:应受程序限制。我认为模拟器可以运行它。
【讨论】:
以上是关于在 amazon-braket 上的大型矩阵上应用 QPE的主要内容,如果未能解决你的问题,请参考以下文章