88from numpy import asarray , ndarray , shape , zeros , empty , average , transpose , dot
99from numpy .linalg import solve
1010
11+ from zero_noise_extrapolation import Richardson_extrapolate
12+
1113import random
1214
1315"""
@@ -313,7 +315,7 @@ def mitigate(self, shots=None, verbose: bool = False) -> float:
313315
314316 self .measurement_results .append (circuit_measurement_results )
315317
316- self .result = richardson_extrapolate (self .noise_amplified_exp_vals , self .noise_amplification_factors )
318+ self .result = Richardson_extrapolate (self .noise_amplified_exp_vals , self .noise_amplification_factors )[ 0 ]
317319
318320 if verbose :
319321 print ("-----" , "\n Error mitigation done" ,
@@ -507,33 +509,4 @@ def pauli_twirl_cnots(qc: QuantumCircuit) -> QuantumCircuit:
507509 optimize1qates = Optimize1qGates ()
508510 pm = PassManager (optimize1qates )
509511
510- return pm .run (new_qc )
511-
512-
513- # Richardson extrapolation:
514- def richardson_extrapolate (E : ndarray , c : ndarray ) -> float :
515- """
516- Code taken from github.com/OpenQuantumComputing/error_mitigation/ -> zero_noise_extrapolation.py and slightly modified
517- :param E: Expectation values
518- :param c: Noise amplification factors
519- :return: Extrapolation to the zero-limit
520- """
521- if isinstance (E , list ):
522- E = asarray (E )
523- if isinstance (E , list ):
524- c = asarray (c )
525-
526- n = E .shape [0 ]
527- if c .shape [0 ] != n :
528- raise ValueError ('E and c must have the same dimension.' )
529- if n <= 1 :
530- raise ValueError ('the dimension of E and c must be larger than 1.' )
531- A = zeros ((n , n ))
532- b = zeros ((n , 1 ))
533- # must sum to 1
534- A [0 , :] = 1
535- b [0 ] = 1
536- for k in range (1 , n ):
537- A [k , :] = c ** k
538- x = solve (A , b )
539- return dot (transpose (E ), x )[0 ]
512+ return pm .run (new_qc )
0 commit comments