<jupyter_start><jupyter_text>[Quantum Fourier Transformation Code used from IBM's Qiskit Tutorials](https://qiskit.org/textbook/ch-algorithms/quantum-fourier-transform.html) Code adapted for Cirq<jupyter_code>import cirq
import random
import matplotlib.pyplot as plt
import numpy as np<jupyter_output><empty_output><jupyter_text>Measure all qubits consistent across all versions<jupyter_code>def set_measure_x(circuit, qubits, n):
    for num in range(n):
        circuit.append(cirq.H(qubits[num]))

def set_measure_y(circuit, qubits, n):
    for num in range(n):
        circuit.append((cirq.Z**(-1/2))(qubits[num]))
        circuit.append(cirq.H(qubits[num]))

def qft_rotations_cirq(circuit, qubits, n):
    if n == 0:
        #print(circuit)
        set_measure_x(circuit, qubits, 4)
        circuit.append(cirq.measure(*qubits, key='this'))
        return circuit
    n -= 1
    circuit.append(cirq.H(qubits[n]))
    for qubit in range(n):
        circuit.append((cirq.CZ**(1/2**(n-qubit)))(qubits[qubit],qubits[n]))
    return qft_rotations_cirq(circuit, qubits, n) 

qubits = cirq.LineQubit.range(4)
circuit = cirq.Circuit()
circuit.append(cirq.X(qubits[0]))
qft_rotations_cirq(circuit, qubits, 4)
##set it to measure Y axis
#set_measure_x(circuit, qubits, 4)
#circuit.append(cirq.measure(*qubits, key='this'))
simulator = cirq.Simulator()
results = simulator.run(circuit , repetitions =100000)
#results = simulator.run(circuit)
print(circuit)
print(results.histogram(key="this"))<jupyter_output>0: ───X───@──────────────────────────────@────────────────────@───────H───H───M('this')───
          │                              │                    │               │
1: ───────┼─────────@────────────────────┼────────@───────H───@^0.5───H───────M───────────
          │         │                    │        │                           │
2: ───────┼─────────┼────────@───────H───@^0.25───@^0.5───H───────────────────M───────────
          │         │        │                                                │
3: ───H───@^(1/8)───@^0.25───@^0.5───H────────────────────────────────────────M───────────
Counter({8: 41087, 12: 41085, 14: 7069, 10: 6912, 13: 1646, 9: 1627, 15: 294, 11: 280})<jupyter_text>Measure 1 specific qubit<jupyter_code>def qft_rotations_cirq(circuit, qubits, n):
    #this implementation is taken from https://qiskit.org/textbook/ch-algorithms/quantum-fourier-transform.html
    #if qubit amount is 0, then do nothing and return
    #print("loop n# %s"%n)
    if n == 0:
        #print(circuit)
        return circuit
    n -= 1
    circuit.append(cirq.H(qubits[n]))
    for qubit in range(n):
        circuit.append((cirq.CZ**(1/2**(n-qubit)))(qubits[qubit],qubits[n]))
        #print(n)
        #print(qubit)
    return qft_rotations_cirq(circuit, qubits, n)     

qubits = cirq.LineQubit.range(4)
circuit = cirq.Circuit()
circuit.append(cirq.X(qubits[0]))
qft_rotations_cirq(circuit, qubits, 4)
##set it to measure Y axis
set_measure_x(circuit, qubits, 4)
##measure the first qubit ---------V
circuit.append(cirq.measure(qubits[0], key='this'))
simulator = cirq.Simulator()
results = simulator.run(circuit , repetitions =1000000)
#results = simulator.run(circuit)
print(circuit)
print(results.histogram(key="this"))<jupyter_output>0: ───X───@──────────────────────────────@────────────────────@───────H───H───M('this')───
          │                              │                    │
1: ───────┼─────────@────────────────────┼────────@───────H───@^0.5───H───────────────────
          │         │                    │        │
2: ───────┼─────────┼────────@───────H───@^0.25───@^0.5───H───────────────────────────────
          │         │        │
3: ───H───@^(1/8)───@^0.25───@^0.5───H────────────────────────────────────────────────────
Counter({1: 1000000})
{'fraction_non_alphanumeric': 0.28044280442804426, 'fraction_numerical': 0.03294675803900896, 'mean_word_length': 3.428238039673279, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '42702', 'license': 'MIT', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1338, 'n_words': 238, 'path': 'Quantum Fourier Transform/Cirq Code/Quantum Fourier Transform Cirq.ipynb', 'repo_name': 'Lilgabz/Quantum-Algorithm-Implementations', 'size': '3794'}