<jupyter_start><jupyter_text>Uncomputation<jupyter_code>%matplotlib inline
from qiskit import *<jupyter_output><empty_output><jupyter_text>To compute $a \wedge b \wedge c$, we need two working qubits. We need to split the computation into two parts: $a \wedge b \wedge c = (a \wedge b) \wedge c$Moreover, it is good practice to clean the working qubit that only contains an intermediate result: that is called uncomputation<jupyter_code>circuit = QuantumCircuit(5, 2)
# Steps to change qubits to 1 for test purposes
_ = circuit.x(0)
_ = circuit.x(1)
_ = circuit.x(2)
_ = circuit.barrier()<jupyter_output><empty_output><jupyter_text>Firstly, we calculate $a \wedge b$ in the first working qubit.<jupyter_code>_ = circuit.ccx(0, 1, 3)
circuit.draw(output="mpl")<jupyter_output><empty_output><jupyter_text>Secondly, we calculate $(a \wedge b) \wedge c$ using both the third qubit and the first working qubit. The second working qubit is the target.<jupyter_code>_ = circuit.ccx(2, 3, 4)
circuit.draw(output="mpl")<jupyter_output><empty_output><jupyter_text>We now uncompute $a \wedge b$ in the first working qubit. That way, we can reuse it later if needed.To do so, we remember that the Toffoli gate is the inverse of itself: $(a \wedge b) \oplus (a \wedge b) = 0$<jupyter_code>_ = circuit.ccx(0, 1, 3)
circuit.draw(output="mpl")<jupyter_output><empty_output><jupyter_text>Lastly, we measure both working qubits: one should contain the AND and the other should always be zero<jupyter_code># Measure (x^y^z) and working qubuit in classical bit
_ = circuit.barrier()
_ = circuit.measure(4, 0)
_ = circuit.measure(3, 1)
circuit.draw(output="mpl")
# Simulate
simulator = Aer.get_backend("qasm_simulator")
job = execute(circuit, backend=simulator, shots=1024)
result = job.result()
counts = result.get_counts()
# Plot results
visualization.plot_histogram(counts)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11246684350132626, 'fraction_numerical': 0.01273209549071618, 'mean_word_length': 6.1169811320754715, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '222679', 'license': 'MIT', 'n_tokens_mistral': 676, 'n_tokens_neox': 633, 'n_words': 201, 'path': 'Basic_Quantum/Uncomputation.ipynb', 'repo_name': 'IvanIsCoding/Quantum', 'size': '1885'}