qiskit-community / qiskit-metal

Quantum Hardware Design. Open-source project for engineers and scientists to design superconducting quantum devices with ease.
https://qiskit-community.github.io/qiskit-metal/
Apache License 2.0
270 stars 201 forks source link

Error in Tutorial 4 Analysis 4.04 New LOM and Fluxonium Example #993

Closed auratrifire closed 2 days ago

auratrifire commented 3 days ago

Information

What is the current behavior?

The running the line: hamiltonian_results = composite_sys.hamiltonian_results(hilbertspace, evals_count=30)

results in an error - TypeError: must be real number, not Qobj

Full error below:

Cell In[17], line 1 hamiltonian_results = composite_sys.hamiltonian_results(hilbertspace, evals_count=30)

File c:\users\aurat\qiskit-metal\qiskit_metal\analyses\quantization\lom_core_analysis.py:1522, in CompositeSystem.hamiltonian_results(self, hilbertspace, evals_count, print_info)

     esys_array[0] = evals
     esys_array[1] = evecs
     f01s, chi_mat = extract_energies(esys_array,
             mode_size=hamiltonian_mat.dims[0])
     f01s = f01s / 1000
     ham_res['fQ_in_Ghz'] = dict(zip(names, f01s))

File c:\users\aurat\qiskit-metal\qiskit_metal\analyses\hamiltonian\states_energies.py:120, in extract_energies(esys_array, mode_size, zero_evals)

      double_excitation_states_mat = np.squeeze(
        np.array([
              double_excitation_states[ii]
              for ii in range(len(double_excitation_states))
         ])).T
         # Find the inner product of each of the target state with each of the
         # eigenvector; hence overlap has dimension of number of eigenvectors x number of target states
       overlap_single = np.absolute(
           np.array((Qobj(evecs_dag_mat) * Qobj(single_excitation_states_mat))))
          overlap_double = np.absolute(
          np.array((Qobj(evecs_dag_mat) * Qobj(double_excitation_states_mat))))
     # find the index of the eigenvector that is closest to each target state
     # hence evec_idx has shape of (number of target states, )

File ~\anaconda3\envs\qubit\lib\site-packages\qutip\core\qobj.py:307, in Qobj.init(self, arg, dims, copy, superrep, isherm, isunitary)

       self._isherm = isherm
       self._isunitary = isunitary
       self._initialize_data(arg, dims, copy)
       if superrep is not None:
       self.superrep = superrep

File ~\anaconda3\envs\qubit\lib\site-packages\qutip\core\qobj.py:295, in Qobj._initialize_data(self, arg, dims, copy)

       self._isunitary = arg._isunitary
       else:
       self._data = _data.create(arg, copy=copy)
       self._dims = Dimensions(
       dims or [[self._data.shape[0]], [self._data.shape[1]]])
       if self._dims.shape != self._data.shape:

File ~\anaconda3\envs\qubit\lib\site-packages\qutip\core\data\convert.pyx:477, in qutip.core.data.convert._create.call()

File ~\anaconda3\envs\qubit\lib\site-packages\qutip\core\data\dense.pyx:43, in qutip.core.data.dense.Dense.init()

Steps to reproduce the problem

Run the code up until part 6 of tutorial 4.04 of Qiskit Metal.

What is the expected behavior?

Give the results of the analyzed Fluxonium hamiltonian.

Suggested solutions

try except statement.

Seems to be some error interfacing with casting an array as a Qobj when it is a scalar.

the following would maybe work, but the solution may not be very general, and try, except statements usually aren't good code:

try: overlap_single = np.absolute( np.array((Qobj(evecs_dag_mat) Qobj(single_excitation_states_mat)))) overlap_double = np.absolute( np.array((Qobj(evecs_dag_mat) Qobj(double_excitation_states_mat))))

except TypeError: overlap_single = np.absolute( np.array((evecs_dag_mat Qobj(single_excitation_states_mat)))) overlap_double = np.absolute( np.array((evecs_dag_mat Qobj(double_excitation_states_mat))))

auratrifire commented 2 days ago

I think I solved the bug,

I replaced:

# Find the inner product of each of the target state with each of the
# eigenvector; hence overlap has dimension of number of eigenvectors x number of target states
overlap_single = np.absolute(
np.array((Qobj(evecs_dag_mat) * Qobj(single_excitation_states_mat))))
overlap_double = np.absolute(
np.array((Qobj(evecs_dag_mat) * Qobj(double_excitation_states_mat))))

with:

# Find the inner product of each of the target state with each of the
# eigenvector; hence overlap has dimension of number of eigenvectors x number of target states

overlap_single = []
overlap_double = []
for u in evecs_dag_mat:
    for v in single_excitation_states_mat:
        overlap_single.append(u*v)
    for w in double_excitation_states_mat:
        overlap_double.append(u*w)

overlap_single = np.reshape(np.absolute(
    np.array(overlap_single)), (*evecs_dag_mat.shape, *single_excitation_states_mat.shape))
overlap_double = np.reshape(np.absolute(
    np.array(overlap_double)), (*evecs_dag_mat.shape, *double_excitation_states_mat.shape))
auratrifire commented 2 days ago

Is there a way I can push my changes to the github?

zlatko-minev commented 2 days ago

Did you try making a pull request?

From: Cody Fan @.> Date: Tuesday, July 2, 2024 at 7:07 PM To: qiskit-community/qiskit-metal @.> Cc: Subscribed @.***> Subject: Re: [qiskit-community/qiskit-metal] Error in Tutorial 4 Analysis 4.04 New LOM and Fluxonium Example (Issue #993)

Is there a way I can push my changes to the github?

— Reply to this email directly, view it on GitHubhttps://github.com/qiskit-community/qiskit-metal/issues/993#issuecomment-2204640928, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADZS5ZTTHLZK6CYH4FPADKLZKMXBHAVCNFSM6AAAAABKGR4OEOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBUGY2DAOJSHA. You are receiving this because you are subscribed to this thread.Message ID: @.***>