def test_qaoa_energy_vs_qiskit(test_problem): G, gamma, beta = test_problem print('no_zz', G.number_of_nodes(), G.number_of_edges(), len(gamma)) sim = QAOAQtreeSimulator(QtreeQAOAComposer) with prof.timing('QTensor energy time'): E = sim.energy_expectation(G, gamma=gamma, beta=beta) assert E gamma, beta = -np.array(gamma) * 2 * np.pi, np.array(beta) * np.pi with prof.timing('Qiskit energy time'): qiskit_E = simulate_qiskit_amps(G, gamma, beta) assert np.isclose(E, qiskit_E)
def test_default_qaoa_energy_vs_qiskit(test_problem): G, gamma, beta = test_problem print('default', G.number_of_nodes(), G.number_of_edges(), len(gamma)) sim = QAOAQtreeSimulator(DefaultQAOAComposer) with prof.timing('QTensor energy time'): E = sim.energy_expectation(G, gamma=gamma, beta=beta) assert E gamma, beta = -np.array(gamma) * 2 * np.pi, np.array(beta) * np.pi with prof.timing('Qiskit energy time'): qiskit_E = simulate_qiskit_amps(G, gamma, beta, shots=QISKIT_SHOTS) assert np.isclose(E, qiskit_E, rtol=REL_MARGIN)
def sliced_contract(x, y, idxs, num): slices = _slices_for_idxs(idxs, x[1], y[1], slice_idx=num) a = x[0][slices[0]] b = y[0][slices[1]] with pyrof.timing(f'\tcontract sliced {num}'): C = contract((a, x[1]), (b, y[1])) return C
def simulate_circ(circuit, n_qubits): buckets, data_dict, bra_vars, ket_vars = qtree.optimizer.circ2buckets( n_qubits, circuit) graph = qtree.graph_model.buckets2graph( buckets, ignore_variables=bra_vars+ket_vars) peo, nghs = utils.get_locale_peo(graph, utils.n_neighbors) peo = qtree.graph_model.indices_to_vars(peo, graph) # place bra and ket variables to beginning, so these variables # will be contracted first #peo, treewidth = qtree.graph_model.get_peo(graph) peo = ket_vars + bra_vars + peo perm_buckets, perm_dict = qtree.optimizer.reorder_buckets(buckets, peo) # extract bra and ket variables from variable list and sort according # to qubit order ket_vars = sorted([perm_dict[idx] for idx in ket_vars], key=str) bra_vars = sorted([perm_dict[idx] for idx in bra_vars], key=str) # Take the subtensor corresponding to the initial state initial_state = target_state = 0 slice_dict = qtree.utils.slice_from_bits(initial_state, ket_vars) slice_dict.update( qtree.utils.slice_from_bits(target_state, bra_vars) ) sliced_buckets = qtree.np_framework.get_sliced_np_buckets( perm_buckets, data_dict, slice_dict) with timing('time_raw', callback=append_to(profile)): result = qtree.optimizer.bucket_elimination( sliced_buckets, qtree.np_framework.process_bucket_np)
C = np.einsum(a,idxa, b,idxb, result_idx) return C def sliced_contract(x, y, idxs, num): slices = _slices_for_idxs(idxs, x[1], y[1], slice_idx=num) a = x[0][slices[0]] b = y[0][slices[1]] with pyrof.timing(f'\tcontract sliced {num}'): C = contract((a, x[1]), (b, y[1])) return C def target_slice(result_idx, idxs, num): slices = _slices_for_idxs(idxs, result_idx, slice_idx=num) return slices with pyrof.timing('contract'): C = contract(x, y) # + [markdown] heading_collapsed=true # ### Maybe sqash dimensions to fit into einsum? # + hidden=true def __contract_bound(A, B): a, idxa = A b, idxb = B contract_idx = set(idxa) & set(idxb) def glue_first(shape): sh = [shape[0] * shape[1]] + list(shape[2:]) return sh
def process_bucket(self, bucket, no_sum=False): indices = [tensor.indices for tensor in bucket] with timing('process bucket time', indices, callback=self._profile_callback): return self.backend.process_bucket(bucket, no_sum=no_sum)