<jupyter_start><jupyter_text>Table of Contents1&nbsp;&nbsp;Creating a circuit1.1&nbsp;&nbsp;QAOA problem graph1.2&nbsp;&nbsp;Universal interface1.2.1&nbsp;&nbsp;Cirq1.2.2&nbsp;&nbsp;Qiskit2&nbsp;&nbsp;Simulate circuits2.1&nbsp;&nbsp;Using qtree2.2&nbsp;&nbsp;Using Cirq2.3&nbsp;&nbsp;Simulate multiple amplitudes3&nbsp;&nbsp;Use lightcone optimisaiton3.1&nbsp;&nbsp;Use qiskit circuit as input4&nbsp;&nbsp;Optimization4.1&nbsp;&nbsp;Get line graph4.2&nbsp;&nbsp;Find ordering  QTensor usage  Creating a circuit<jupyter_code>import qiskit
qiskit.__qiskit_version__
# Qtensor branch: dev
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
from qtensor import QAOAComposer
from qtensor import CirqQAOAComposer, QtreeQAOAComposer<jupyter_output><empty_output><jupyter_text>QAOA problem graph<jupyter_code>G = nx.erdos_renyi_graph(4, 2/(5-1))
nx.draw_kamada_kawai(G, with_labels=True)<jupyter_output><empty_output><jupyter_text>Universal interface Cirq<jupyter_code>p = 1
cirq_qaoa = CirqQAOAComposer(G, gamma=[.1]*p, beta=[.2]*p)
cirq_qaoa.ansatz_state()
cirq_qaoa.circuit<jupyter_output><empty_output><jupyter_text>Qiskit<jupyter_code>from qtensor import QiskitQAOAComposer

p = 1
qiskit_qaoa = QiskitQAOAComposer(G, gamma=[.1]*p, beta=[.2]*p)
qiskit_qaoa.ansatz_state()
qiskit_qaoa.circuit.draw('mpl')<jupyter_output><empty_output><jupyter_text>Simulate circuits Using qtree<jupyter_code>from qtensor import QtreeSimulator
com = QtreeQAOAComposer(G, gamma=[.1]*p, beta=[.2]*p)
com.ansatz_state()
com.circuit
sim = QtreeSimulator()
first_amp = sim.simulate(com.circuit)

first_amp<jupyter_output><empty_output><jupyter_text>Using Cirq<jupyter_code>from qtensor import CirqSimulator
cirq_sim = CirqSimulator()
cirq_measure = cirq_sim.simulate(cirq_qaoa.circuit)

cirq_first_amp = cirq_measure.final_state[0]
cirq_first_amp<jupyter_output><empty_output><jupyter_text>Simulate multiple amplitudes<jupyter_code>state = sim.simulate_batch(com.circuit, batch_vars=com.n_qubits)

# Plot real vs imaginary parts of Qtree vs Cirq simulator results
plt.plot(np.real(state), np.imag(state))
plt.scatter(*[f(cirq_measure.final_state) for f in [np.real, np.imag]], c='red')

# Check for correctness
assert np.allclose(state, cirq_measure.final_state)<jupyter_output><empty_output><jupyter_text>Use lightcone optimisaitonSuppose we are interested in an expectation value of particular operator in a state $|\psi\rangle = \hat U | 0\rangle$. We can use the fact that in the expression$$\langle \psi | \hat E | \psi \rangle = \langle 0 | \hat U^\dagger \hat E \hat U |0\rangle$$a lot of operators from $\hat U$ cancel out.<jupyter_code>p = 2
qiskit_qaoa = QiskitQAOAComposer(G, gamma=[.1]*p, beta=[.2]*p)
qiskit_qaoa.energy_expectation_lightcone((0,2))
qiskit_qaoa.circuit.draw('mpl')<jupyter_output><empty_output><jupyter_text>QTensor provides native support for canceling out the commuting operators<jupyter_code>com = QtreeQAOAComposer(G, gamma=[.1]*p, beta=[.2]*p)
com.energy_expectation_lightcone(list(G.edges())[0])

from qtensor.QAOASimulator import QAOAQtreeSimulator
qaoa_sim = QAOAQtreeSimulator(QtreeQAOAComposer)

qaoa_sim.energy_expectation(G, gamma=[.1]*p, beta=[.2]*p)<jupyter_output><empty_output><jupyter_text>Use qiskit circuit as input<jupyter_code>from qtree.operators import from_qiskit_circuit
n, qc = from_qiskit_circuit(qiskit_qaoa.circuit)
all_gates = sum(qc, [])
sim.simulate(all_gates)<jupyter_output><empty_output><jupyter_text>Optimization<jupyter_code>from qtensor.optimisation.TensorNet import QtreeTensorNet
from qtensor.optimisation.Optimizer import TreeTrimSplitter, OrderingOptimizer<jupyter_output><empty_output><jupyter_text>Get line graph<jupyter_code>tn = QtreeTensorNet.from_qtree_gates(all_gates)
line_g = tn.get_line_graph()

nx.draw_kamada_kawai(line_g, with_labels=True, node_color='yellow')<jupyter_output><empty_output><jupyter_text>Find ordering<jupyter_code>opt = OrderingOptimizer()
peo, tn = opt.optimize(tn)
print('Elimination order:', opt.peo)
opt.treewidth
opt = TreeTrimSplitter(tw_bias=26)
peo, slice_vars, tn = opt.optimize(tn)
opt.treewidth
print('Slice vars', slice_vars)<jupyter_output>2020-10-16 01:20:14.433 | INFO     | qtensor.optimisation.Optimizer:_get_max_tw:88 - Memory available: 7727304704
2020-10-16 01:20:14.434 | INFO     | qtensor.optimisation.Optimizer:optimize:121 - Maximum treewidth: 28
2020-10-16 01:20:14.436 | INFO     | qtensor.optimisation.Optimizer:_split_graph:160 - Treewidth: 4
2020-10-16 01:20:14.438 | INFO     | qtensor.optimisation.Optimizer:_split_graph:161 - Target treewidth: 2
{'fraction_non_alphanumeric': 0.11939645746774546, 'fraction_numerical': 0.03214520008746993, 'mean_word_length': 8.529166666666667, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '634983', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1759, 'n_words': 290, 'path': 'examples/QTensor usage.ipynb', 'repo_name': 'marwahaha/QTensor', 'size': '4573'}