def test_average_data_list_obverable(self): """Test average_data for list observable input.""" qr = qiskit.QuantumRegister(3) cr = qiskit.ClassicalRegister(3) qc = qiskit.QuantumCircuit(qr, cr, name="qc") qc.h(qr[0]) qc.cx(qr[0], qr[1]) qc.cx(qr[0], qr[2]) qc.measure(qr[0], cr[0]) qc.measure(qr[1], cr[1]) qc.measure(qr[2], cr[2]) shots = 10000 backend = BasicAer.get_backend('qasm_simulator') result = qiskit.execute(qc, backend, shots=shots).result() counts = result.get_counts(qc) observable = [1, -1, -1, 1, -1, 1, 1, -1] mean_zzz = average_data(counts=counts, observable=observable) observable = [1, 1, 1, 1, -1, -1, -1, -1] mean_zii = average_data(counts, observable) observable = [1, 1, -1, -1, 1, 1, -1, -1] mean_izi = average_data(counts, observable) observable = [1, 1, -1, -1, -1, -1, 1, 1] mean_zzi = average_data(counts, observable) self.assertAlmostEqual(mean_zzz, 0, places=1) self.assertAlmostEqual(mean_zii, 0, places=1) self.assertAlmostEqual(mean_izi, 0, places=1) self.assertAlmostEqual(mean_zzi, 1, places=1)
def get_magnetization(result, circuits: list) -> float: observable_first = {'00': 1, '01': -1, '10': 1, '11': -1} observable_second = {'00': 1, '01': 1, '10': -1, '11': -1} res = (average_data(result.get_counts(circuits[0]), observable_first) + average_data(result.get_counts(circuits[0]), observable_second)) / 2 return res
def test_average_data_dict_obverable(self): """Test average_data for dictionary observable input""" qr = qiskit.QuantumRegister(2) cr = qiskit.ClassicalRegister(2) qc = qiskit.QuantumCircuit(qr, cr, name="qc") qc.h(qr[0]) qc.cx(qr[0], qr[1]) qc.measure(qr[0], cr[0]) qc.measure(qr[1], cr[1]) shots = 10000 backend = BasicAer.get_backend('qasm_simulator') result = qiskit.execute(qc, backend, shots=shots).result() counts = result.get_counts(qc) observable = {"00": 1, "11": 1, "01": -1, "10": -1} mean_zz = average_data(counts=counts, observable=observable) observable = {"00": 1, "11": -1, "01": 1, "10": -1} mean_zi = average_data(counts, observable) observable = {"00": 1, "11": -1, "01": -1, "10": 1} mean_iz = average_data(counts, observable) self.assertAlmostEqual(mean_zz, 1, places=1) self.assertAlmostEqual(mean_zi, 0, places=1) self.assertAlmostEqual(mean_iz, 0, places=1)
def get_magnetization(qubit_quantity: int, result, circuits: list) -> float: res = 0 if qubit_quantity == 2: observable_first = {'00': 1, '01': -1, '10': 1, '11': -1} observable_second = {'00': 1, '01': 1, '10': -1, '11': -1} res = (average_data(result.get_counts(circuits[0]), observable_first) + average_data(result.get_counts(circuits[0]), observable_second)) / 2 if qubit_quantity == 3: observable_first = {'000': 1, '100': 1, '011': -1, '111': -1, '001': -1, '010': 1, '101': -1, '110': 1, } observable_second = {'000': 1, '100': 1, '011': -1, '111': -1, '001': 1, '010': -1, '101': 1, '110': -1, } observable_third = {'000': 1, '100': -1, '011': 1, '111': -1, '001': 1, '010': 1, '101': -1, '110': -1, } res = (average_data(result.get_counts(circuits[0]), observable_first) + average_data(result.get_counts(circuits[0]), observable_second) + average_data(result.get_counts(circuits[0]), observable_third)) / 2 if qubit_quantity == 4: observable_first = {'0000': 1, '1110': 1, '1000': 1, '0100': 1, '0010': 1, '1100': 1, '0110': 1, '1010': 1, '0001': -1, '1111': -1, '1001': -1, '0101': -1, '0011': -1, '1101': -1, '0111': -1, '1011': -1} #***1 observable_second = {'0000': 1, '1101': 1, '1000': 1, '0100': 1, '0001': 1, '1100': 1, '0101': 1, '1001': 1, '0010': -1, '1111': -1, '1010': -1, '0110': -1, '0011': -1, '1110': -1, '0111': -1, '1011': -1} #**1* observable_third = {'0000': 1, '1011': 1, '1000': 1, '0010': 1, '0001': 1, '1010': 1, '0011': 1, '1001': 1, '0100': -1, '1111': -1, '1100': -1, '0110': -1, '0101': -1, '1110': -1, '0111': -1, '1101': -1} #*1** observable_fourth = {'0000': 1, '0111': 1, '0100': 1, '0010': 1, '0001': 1, '0110': 1, '0011': 1, '0101': 1, '1000': -1, '1111': -1, '1100': -1, '1010': -1, '1001': -1, '1110': -1, '1011': -1, '1101': -1} #1*** res = (average_data(result.get_counts(circuits[0]), observable_first) + average_data(result.get_counts(circuits[0]), observable_second) + average_data(result.get_counts(circuits[0]), observable_third) + average_data(result.get_counts(circuits[0]), observable_fourth)) / 2 return res
def get_energy(exchange, bj, result1, circuits: list) -> float: """ Calculate Heisenberg energy :param exchange: exchange constant :param result: a job object storing the results of quantum calculation :param bj: B/J :param circuits: list containing XX YY and ZZ circuits :param codes: :return: """ codes = {'00': 1, '01': -1, '10': -1, '11': 1} res = exchange / 4 * ( average_data(result1.get_counts(circuits[0]), codes) + average_data(result1.get_counts(circuits[1]), codes) + average_data(result1.get_counts(circuits[2]), codes) ) + bj * exchange * get_magnetization(result1, [circuits[2]]) return res
def get_fitness(result, circuits: list, codes1: dict, codes2: dict, codes: dict) -> float: """ Calculate fitness :param exchange: exchange constant :param result: a job object storing the results of quantum calculation :param circuits: list containing XX YY and ZZ circuits :param codes: :return: """ res = abs(average_data(result.get_counts(circuits[0]), codes1))/2\ +abs(average_data(result.get_counts(circuits[0]), codes2))/2\ +abs(average_data(result.get_counts(circuits[1]), codes1))/2\ +abs(average_data(result.get_counts(circuits[1]), codes2))/2\ +abs(average_data(result.get_counts(circuits[2]), codes1))/2\ +abs(average_data(result.get_counts(circuits[2]), codes2))/2\ +abs(average_data(result.get_counts(circuits[0]), codes)+1)\ +abs(average_data(result.get_counts(circuits[1]), codes)+1)\ +abs(average_data(result.get_counts(circuits[2]), codes)+1)\ +abs(average_data(result.get_counts(circuits[3]), codes))\ +abs(average_data(result.get_counts(circuits[4]), codes))\ +abs(average_data(result.get_counts(circuits[5]), codes))\ +abs(average_data(result.get_counts(circuits[6]), codes))\ +abs(average_data(result.get_counts(circuits[7]), codes))\ +abs(average_data(result.get_counts(circuits[8]), codes)) return res
def get_energy(qubit_quantity: int, exchange, bj, result, circuits: list) -> float: """ Calculate Heisenberg energy :param qubit_quantity: qubit quantity :param exchange: exchange constant :param result: a job object storing the results of quantum calculation :param bj: B/J :param circuits: list containing XX YY and ZZ circuits :return: """ res = 0 if qubit_quantity == 2: codes = {'00': 1, '01': -1, '10': -1, '11': 1} res = exchange/4 * (average_data(result.get_counts(circuits[0]), codes) + average_data(result.get_counts(circuits[1]), codes) + average_data(result.get_counts(circuits[2]), codes)) + \ bj * exchange * get_magnetization(qubit_quantity, result, [circuits[2]]) if qubit_quantity == 3: first_correlated = {'000': 1, '100': 1, '011': 1, '111': 1, '001': -1, '010': -1, '101': -1, '110': -1} second_correlated = {'000': 1, '001': 1, '110': 1, '111': 1, '100': -1, '010': -1, '101': -1, '011': -1} # smart_correlated = { # '12', {'000': 1, '100': 1, '011': 1, '111': 1, '001': -1, '010': -1, '101': -1, '110': -1, }, # '23', {'000': 1, '001': 1, '110': 1, '111': 1, '100': -1, '010': -1, '101': -1, '011': -1, } # } res = exchange/4 * (average_data(result.get_counts(circuits[0]), first_correlated) + average_data(result.get_counts(circuits[1]), first_correlated) + average_data(result.get_counts(circuits[2]), first_correlated) + average_data(result.get_counts(circuits[0]), second_correlated) + average_data(result.get_counts(circuits[1]), second_correlated) + average_data(result.get_counts(circuits[2]), second_correlated)) + \ bj * exchange * get_magnetization(qubit_quantity, result, [circuits[2]]) if qubit_quantity == 4: first_correlated = {'0000': 1, '1100': 1, '1000': 1, '0100': 1, '0011': 1, '1111': 1, '1011': 1, '0111': 1, '0001': -1, '1101': -1, '1001': -1, '0101': -1, '0010': -1, '1110': -1, '1010': -1, '0110': -1} #**11 second_correlated = {'0000': 1, '1001': 1, '1000': 1, '0001': 1, '0110': 1, '1111': 1, '1110': 1, '0111': 1, '0100': -1, '1101': -1, '1100': -1, '0101': -1, '0010': -1, '1011': -1, '1010': -1, '0011': -1,}#*11* third_correlated = {'0000': 1, '0011': 1, '0010': 1, '0001': 1, '1100': 1, '1111': 1, '1110': 1, '1101': 1, '1000': -1, '1011': -1, '1010': -1, '1001': -1, '0100': -1, '0111': -1, '0110': -1, '0101': -1,}#11** res = exchange/4 * (average_data(result.get_counts(circuits[0]), first_correlated) + average_data(result.get_counts(circuits[1]), first_correlated) + average_data(result.get_counts(circuits[2]), first_correlated) + average_data(result.get_counts(circuits[0]), second_correlated) + average_data(result.get_counts(circuits[1]), second_correlated) + average_data(result.get_counts(circuits[2]), second_correlated) + average_data(result.get_counts(circuits[0]), third_correlated) + average_data(result.get_counts(circuits[1]), third_correlated) + average_data(result.get_counts(circuits[2]), third_correlated)) + \ bj * exchange * get_magnetization(qubit_quantity, result, [circuits[2]]) return res