def EstimatedVaR(x): cdf_objective = FixedValueComparator(agg.num_sum_qubits, x + 1, geq=False) multivariate_var = MultivariateProblem(u, agg, cdf_objective) num_eval_qubits = 4 ae_var = AmplitudeEstimation(num_eval_qubits, multivariate_var) result_var = ae_var.run( quantum_instance=BasicAer.get_backend('statevector_simulator')) #print("result_var['estimation']: ", result_var['estimation']) return result_var['estimation']
def test_fixed_value_comparator(self, num_state_qubits, value, geq): """ fixed value comparator test """ # initialize weighted sum operator factory comp = Comparator(num_state_qubits, value, geq) # initialize circuit q = QuantumRegister(num_state_qubits + 1) if comp.required_ancillas() > 0: q_a = QuantumRegister(comp.required_ancillas()) qc = QuantumCircuit(q, q_a) else: q_a = None qc = QuantumCircuit(q) # set equal superposition state qc.h(q[:num_state_qubits]) # build circuit comp.build(qc, q, q_a) # run simulation job = execute(qc, BasicAer.get_backend('statevector_simulator'), shots=1) for i, s_a in enumerate(job.result().get_statevector()): prob = np.abs(s_a)**2 if prob > 1e-6: # equal superposition self.assertEqual(True, np.isclose(1.0, prob * 2.0**num_state_qubits)) b_value = '{0:b}'.format(i).rjust(qc.width(), '0') x = int(b_value[(-num_state_qubits):], 2) comp_result = int(b_value[-num_state_qubits - 1], 2) if geq: self.assertEqual(x >= value, comp_result == 1) else: self.assertEqual(x < value, comp_result == 1)
# plt.xticks(size=15) # plt.yticks([0, 0.25, 0.5, 0.75, 1], size=15) # plt.title('Expected Loss', size=15) # plt.ylabel('Probability', size=15) # plt.ylim((0,1)) # plt.grid() # plt.show() #partie 3 estimation de CDF (cumulative distribution function) en utilisant QAE print("----------estimaton de CDF-------------------------") # fixer la valeur x pour estimer le CDF x_eval = 2 # définition du circuit de comparaison cdf_objective = FixedValueComparator(agg.num_sum_qubits, x_eval + 1, geq=False) # définition du "Multivariate Problem" multivariate_cdf = MultivariateProblem(u, agg, cdf_objective) # obtenir le nombre de qubits nécessaires pour représenter le problème num_qubits = multivariate_cdf.num_target_qubits # obtenir le nombre de qubits auxiliaires nécessaires pour représenter le problème num_ancillas = multivariate_cdf.required_ancillas() # construire le circuit q = QuantumRegister(num_qubits, name='q') q_a = QuantumRegister(num_ancillas, name='q_a') qc = QuantumCircuit(q, q_a) multivariate_cdf.build(qc, q, q_a)