0
$\begingroup$

For a circuit with a classical register like in the image, I cannot apply StateTomographyFitter(result, tomography_circuits).fit() method for the second and third qubit.

enter image description here

Specifically, I get the "array must not contain infs or NaNs" error. This doesn't seem to be an issue when there is no additional classical register, or when I apply tomography to only one qubit. My counts are in the form of:

[{'0 00': 29,
  '0 01': 3,
  '0 10': 1,
  '0 11': 21,
  '1 00': 24,
  '1 01': 2,
  '1 10': 3,
  '1 11': 17},
 {'0 00': 10,
  '0 01': 13,
  '0 10': 14,
  '0 11': 16,
  '1 00': 15,
  '1 01': 16,
  '1 10': 7,
  '1 11': 9},
 {'0 00': 15,
  '0 01': 13,
  '0 10': 9,
  '0 11': 5,
  '1 00': 23,
  '1 01': 14,
  '1 10': 10,
  '1 11': 11},
 {'0 00': 12,
  '0 01': 15,
  '0 10': 18,
  '0 11': 11,
  '1 00': 7,
  '1 01': 7,
  '1 10': 18,
  '1 11': 12},
 {'0 00': 2,
  '0 01': 22,
  '0 10': 25,
  '0 11': 2,
  '1 01': 26,
  '1 10': 21,
  '1 11': 2},
 {'0 00': 21,
  '0 01': 18,
  '0 10': 7,
  '0 11': 5,
  '1 00': 16,
  '1 01': 20,
  '1 10': 7,
  '1 11': 6},
 {'0 00': 20,
  '0 01': 7,
  '0 10': 19,
  '0 11': 9,
  '1 00': 18,
  '1 01': 5,
  '1 10': 20,
  '1 11': 2},
 {'0 00': 16,
  '0 01': 3,
  '0 10': 19,
  '0 11': 3,
  '1 00': 22,
  '1 01': 7,
  '1 10': 17,
  '1 11': 13},
 {'0 00': 39,
 '0 11': 9, 
'1 00': 42, 
'1 11': 10}]

From what I could find on the Internet, the problem may be caused by the gap in the counts. Here is an image of one of the tomography circuits: enter image description here

Since I need the results from the first qubit, I cannot remove the classical register. Is there a way to remove the space in the count, or a way around to apply state tomography?

Here is my code:

from qiskit import *
from qiskit.ignis.verification.tomography import state_tomography_circuits, StateTomographyFitter
import numpy as np
qr = QuantumRegister(3) # create register to store bits
cr = ClassicalRegister(1)
circuit = QuantumCircuit(qr, cr) # creates the circuit
circuit.h(2)
circuit.cx(qr[2], qr[1])
circuit.barrier()
pi = math.pi
circuit.ry(pi/3,0)
circuit.cx(qr[1], qr[0])
circuit.measure(0,0)
circuit.barrier()
circuit.x(qr[1]).c_if(cr,1)
circuit.x(qr[2]).c_if(cr,1)
simulator = Aer.get_backend('qasm_simulator')
tomography_circs = state_tomography_circuits(circuit, [qr[1], qr[2]])
backend = Aer.get_backend('qasm_simulator')
result = execute(tomography_circs, backend = backend, shots = 100).result()
result.get_counts()
fitter = StateTomographyFitter(result, tomography_circs).fit()

And here is the full error:

    ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-4d89f51bbd11> in <module>
----> 1 fitter = StateTomographyFitter(result, tomography_circs).fit()
      2 """
      3 from qiskit.quantum_info import state_fidelity
      4 phi_plus = np.array([1, 0, 0, 1])/np.sqrt(2)   # | Phi^+ >
      5 simulated_output_fidelity = state_fidelity(fitter, phi_plus)

~\anaconda3\lib\site-packages\qiskit\ignis\verification\tomography\fitters\state_fitter.py in fit(self, method, standard_weights, beta, **kwargs)
    120             \text{vec}(\text{rho}) - \text{data}||_2`.
    121         """
--> 122         return super().fit(method, standard_weights, beta,
    123                            trace=1, psd=True, **kwargs)

~\anaconda3\lib\site-packages\qiskit\ignis\verification\tomography\fitters\base_fitter.py in fit(self, method, standard_weights, beta, psd, trace, trace_preserving, **kwargs)
    206                 method = 'lstsq'
    207         if method == 'lstsq':
--> 208             return lstsq_fit(data, basis_matrix,
    209                              weights=weights,
    210                              psd=psd,

~\anaconda3\lib\site-packages\qiskit\ignis\verification\tomography\fitters\lstsq_fit.py in lstsq_fit(data, basis_matrix, weights, psd, trace)
     98 
     99     # Perform least squares fit using Scipy.linalg lstsq function
--> 100     rho_fit, _, _, _ = lstsq(meas_matrix, exp_values)
    101 
    102     # Reshape fit to a density matrix

~\anaconda3\lib\site-packages\scipy\linalg\basic.py in lstsq(a, b, cond, overwrite_a, overwrite_b, check_finite, lapack_driver)
   1156     """
   1157     a1 = _asarray_validated(a, check_finite=check_finite)
-> 1158     b1 = _asarray_validated(b, check_finite=check_finite)
   1159     if len(a1.shape) != 2:
   1160         raise ValueError('Input array a should be 2-D')

~\anaconda3\lib\site-packages\scipy\_lib\_util.py in _asarray_validated(a, check_finite, sparse_ok, objects_ok, mask_ok, as_inexact)
    244             raise ValueError('masked arrays are not supported')
    245     toarray = np.asarray_chkfinite if check_finite else np.asarray
--> 246     a = toarray(a)
    247     if not objects_ok:
    248         if a.dtype is np.dtype('O'):

~\anaconda3\lib\site-packages\numpy\lib\function_base.py in asarray_chkfinite(a, dtype, order)
    486     a = asarray(a, dtype=dtype, order=order)
    487     if a.dtype.char in typecodes['AllFloat'] and not np.isfinite(a).all():
--> 488         raise ValueError(
    489             "array must not contain infs or NaNs")
    490     return a

ValueError: array must not contain infs or NaNs
$\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.