1
$\begingroup$

I'm running a QPE algorithm on the amazon-braket but it can only apply on a 22 or 44 matrix, when I want to expand it into a 5*5 or more, it will come an error. As I know, there is no theoretical limitation on the size of matrix QPE can solve.

Here is the main part of the code:

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

And when it comes to a 6*6 matrix,

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)

the error report is like:

---------------------------------------------------------------------------
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

So I'm wondering if it's the packages' problem or the program problem or any other had met the similar problem as I?

Thanks in advance!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.