# quantum-algorithms

git clone git://git.codymlewis.com/quantum-algorithms.git

```commit 95d93aec55bfd8e1a9f340ebb22803bfe0fa2f54
Author: Cody Lewis <cody@codymlewis.com>
Date:   Fri, 29 May 2020 19:37:44 +1000

Initial commit

Diffstat:
Afanout | 47+++++++++++++++++++++++++++++++++++++++++++++++
Arequirements.txt | 1+
Aswap | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
```
```4 files changed, 115 insertions(+), 0 deletions(-)
@@ -0,0 +1,16 @@
+# Quantum Algorithms
+Some of my implementations of some quantum computing algorithms
+
+## Requirements
+- python 3.7+
+- pip
+
+## Installation
+```sh
+pip install -r requirements.txt
+```
+
+## Running
+```sh
+python <filename>
+```
diff --git a/fanout b/fanout
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+'''
+A simple quantum circuit that replicates the state of a qubit to N others
+'''
+
+import argparse
+
+from qiskit import QuantumCircuit, Aer, execute
+
+def to_statevector(a):
+    '''Convert a binary string into its representative qubit statevector'''
+    if a == "0":
+        return [1, 0]
+    return [0, 1]
+
+def create_circuit(in_state, n):
+    '''Construct the circuit with the input states set'''
+    qcircuit = QuantumCircuit(n)
+    qcircuit.initialize(to_statevector(in_state[0]), 0)
+    for i in range(1, n):
+        qcircuit.cnot(0, i)
+    meas = QuantumCircuit(n, n)
+    meas.barrier(range(n))
+    meas.measure(range(n), range(n))
+    return qcircuit + meas
+
+
+if __name__ == '__main__':
+    PARSER = argparse.ArgumentParser(
+        description="A simple quantum circuit that replicates the state of a qubit to N others"
+    )
+        'in_state', metavar='INPUT_STATE', type=str,
+        help='Binary string representing the input state of the qubit'
+    )
+        'n', metavar='N', type=int,
+        help='The number of qubits to fanout to'
+    )
+    ARGS = PARSER.parse_args()
+    QCIRC = create_circuit(ARGS.in_state, ARGS.n)
+    print("Created circuit:")
+    print(QCIRC.draw())
+    BACKEND_SIM = Aer.get_backend('qasm_simulator')
+    RESULT = execute(QCIRC, BACKEND_SIM).result()
+    print(f'Result: |{ARGS.in_state}> -> |{max(RESULT.get_counts())[::-1]}>')
diff --git a/requirements.txt b/requirements.txt
@@ -0,0 +1 @@
+qiskit
diff --git a/swap b/swap
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+
+'''
+A simple quantum circuit that swaps qubits sequentially
+'''
+
+import argparse
+
+from qiskit import QuantumCircuit, Aer, execute
+
+def swap_qubits(qcircuit, i, j):
+    '''Performs that transform swapping the value in q_i with q_j'''
+    qcircuit.cnot(i, j)
+    qcircuit.cnot(j, i)
+    qcircuit.cnot(i, j)
+
+def to_statevector(a):
+    '''Convert a binary string into its representative qubit statevector'''
+    if a == "0":
+        return [1, 0]
+    return [0, 1]
+
+def create_circuit(in_states):
+    '''Construct the circuit with the input states set'''
+    n = len(in_states)
+    qcircuit = QuantumCircuit(n)
+    for i in range(n):
+        qcircuit.initialize(to_statevector(in_states[i]), i)
+    for i in range(n - 1):
+        swap_qubits(qcircuit, i, i + 1)
+    meas = QuantumCircuit(n, n)
+    meas.barrier(range(n))
+    meas.measure(range(n), range(n))
+    return qcircuit + meas
+
+
+if __name__ == '__main__':
+    PARSER = argparse.ArgumentParser(
+        description="A simple quantum circuit that swaps qubits sequentially"
+    )