Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
3,592 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
project(quantum) | ||
cmake_minimum_required(VERSION 2.8) | ||
|
||
set(CMAKE_BUILD_TYPE debug) | ||
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=c++11") | ||
set(CMAKE_CXX_FLAGS_RELEASE "-O3") | ||
|
||
add_subdirectory(lib) | ||
add_subdirectory(test) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# CMAKE generated file: DO NOT EDIT! | ||
# Generated by "Unix Makefiles" Generator, CMake Version 2.8 | ||
|
||
# Default target executed when no arguments are given to make. | ||
default_target: all | ||
.PHONY : default_target | ||
|
||
#============================================================================= | ||
# Special targets provided by cmake. | ||
|
||
# Disable implicit rules so canonical targets will work. | ||
.SUFFIXES: | ||
|
||
# Remove some rules from gmake that .SUFFIXES does not remove. | ||
SUFFIXES = | ||
|
||
.SUFFIXES: .hpux_make_needs_suffix_list | ||
|
||
# Suppress display of executed commands. | ||
$(VERBOSE).SILENT: | ||
|
||
# A target that is always out of date. | ||
cmake_force: | ||
.PHONY : cmake_force | ||
|
||
#============================================================================= | ||
# Set environment variables for the build. | ||
|
||
# The shell in which to execute make rules. | ||
SHELL = /bin/sh | ||
|
||
# The CMake executable. | ||
CMAKE_COMMAND = /usr/bin/cmake | ||
|
||
# The command to remove a file. | ||
RM = /usr/bin/cmake -E remove -f | ||
|
||
# Escaping for special characters. | ||
EQUALS = = | ||
|
||
# The top-level source directory on which CMake was run. | ||
CMAKE_SOURCE_DIR = /home/kcoltin/Documents/misc/quantum/cpp/quantum | ||
|
||
# The top-level build directory on which CMake was run. | ||
CMAKE_BINARY_DIR = /home/kcoltin/Documents/misc/quantum/cpp/quantum/build | ||
|
||
#============================================================================= | ||
# Targets provided globally by CMake. | ||
|
||
# Special rule for the target edit_cache | ||
edit_cache: | ||
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running interactive CMake command-line interface..." | ||
/usr/bin/cmake -i . | ||
.PHONY : edit_cache | ||
|
||
# Special rule for the target edit_cache | ||
edit_cache/fast: edit_cache | ||
.PHONY : edit_cache/fast | ||
|
||
# Special rule for the target rebuild_cache | ||
rebuild_cache: | ||
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." | ||
/usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) | ||
.PHONY : rebuild_cache | ||
|
||
# Special rule for the target rebuild_cache | ||
rebuild_cache/fast: rebuild_cache | ||
.PHONY : rebuild_cache/fast | ||
|
||
# The main all target | ||
all: cmake_check_build_system | ||
$(CMAKE_COMMAND) -E cmake_progress_start /home/kcoltin/Documents/misc/quantum/cpp/quantum/build/CMakeFiles /home/kcoltin/Documents/misc/quantum/cpp/quantum/build/CMakeFiles/progress.marks | ||
$(MAKE) -f CMakeFiles/Makefile2 all | ||
$(CMAKE_COMMAND) -E cmake_progress_start /home/kcoltin/Documents/misc/quantum/cpp/quantum/build/CMakeFiles 0 | ||
.PHONY : all | ||
|
||
# The main clean target | ||
clean: | ||
$(MAKE) -f CMakeFiles/Makefile2 clean | ||
.PHONY : clean | ||
|
||
# The main clean target | ||
clean/fast: clean | ||
.PHONY : clean/fast | ||
|
||
# Prepare targets for installation. | ||
preinstall: all | ||
$(MAKE) -f CMakeFiles/Makefile2 preinstall | ||
.PHONY : preinstall | ||
|
||
# Prepare targets for installation. | ||
preinstall/fast: | ||
$(MAKE) -f CMakeFiles/Makefile2 preinstall | ||
.PHONY : preinstall/fast | ||
|
||
# clear depends | ||
depend: | ||
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 | ||
.PHONY : depend | ||
|
||
#============================================================================= | ||
# Target rules for targets named quantum | ||
|
||
# Build rule for target. | ||
quantum: cmake_check_build_system | ||
$(MAKE) -f CMakeFiles/Makefile2 quantum | ||
.PHONY : quantum | ||
|
||
# fast build rule for target. | ||
quantum/fast: | ||
$(MAKE) -f lib/CMakeFiles/quantum.dir/build.make lib/CMakeFiles/quantum.dir/build | ||
.PHONY : quantum/fast | ||
|
||
#============================================================================= | ||
# Target rules for targets named run_tests | ||
|
||
# Build rule for target. | ||
run_tests: cmake_check_build_system | ||
$(MAKE) -f CMakeFiles/Makefile2 run_tests | ||
.PHONY : run_tests | ||
|
||
# fast build rule for target. | ||
run_tests/fast: | ||
$(MAKE) -f test/CMakeFiles/run_tests.dir/build.make test/CMakeFiles/run_tests.dir/build | ||
.PHONY : run_tests/fast | ||
|
||
# Help Target | ||
help: | ||
@echo "The following are some of the valid targets for this Makefile:" | ||
@echo "... all (the default if no target is provided)" | ||
@echo "... clean" | ||
@echo "... depend" | ||
@echo "... edit_cache" | ||
@echo "... rebuild_cache" | ||
@echo "... quantum" | ||
@echo "... run_tests" | ||
.PHONY : help | ||
|
||
|
||
|
||
#============================================================================= | ||
# Special targets to cleanup operation of make. | ||
|
||
# Special rule to run CMake to check the build system integrity. | ||
# No rule that depends on this can have commands that come from listfiles | ||
# because they might be regenerated. | ||
cmake_check_build_system: | ||
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 | ||
.PHONY : cmake_check_build_system | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef ALGORITHMS_H | ||
#define ALGORITHMS_H | ||
|
||
#include "qubit_system.h" | ||
#include <string> | ||
|
||
namespace quantum_algorithm_simulator { | ||
|
||
void qft (QubitSystem *q); | ||
int grover_search (const std::string &match_text, const std::string *list, | ||
int n); | ||
int grover_invert (int (*f) (int), int y, int n); | ||
int shor_factor (int n); | ||
|
||
} | ||
|
||
#endif | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// gate_factory.h | ||
|
||
#ifndef GATE_FACTORY_H | ||
#define GATE_FACTORY_H | ||
|
||
#include "quantum_gate.h" | ||
#include <functional> | ||
|
||
namespace quantum_algorithm_simulator { | ||
|
||
// Namespace containing factory methods for specific types of gates | ||
namespace qgates { | ||
|
||
QuantumGate x_gate (); | ||
QuantumGate y_gate (); | ||
QuantumGate z_gate (); | ||
QuantumGate hadamard_gate (); | ||
QuantumGate hadamard_gate (int n); | ||
QuantumGate phase_shift_gate (double theta); | ||
QuantumGate swap_gate (); | ||
QuantumGate cnot_gate (); | ||
QuantumGate controlled_gate (const QuantumGate &U); | ||
QuantumGate toffoli_gate (); | ||
QuantumGate fredkin_gate (); | ||
QuantumGate function_gate (int (*f) (int), int m, int k); | ||
QuantumGate function_gate (std::function<int (int)> f, int m, int k); | ||
QuantumGate grover_diffusion_operator (int n); | ||
|
||
} | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#ifndef QUANTUM_GATE_H | ||
#define QUANTUM_GATE_H | ||
|
||
#include <complex> | ||
|
||
namespace quantum_algorithm_simulator { | ||
|
||
class QubitSystem; | ||
|
||
// Represents a quantum logic gate. | ||
class QuantumGate { | ||
private: | ||
// Number of qubits that the gate operates on. A gate operating on n qubits is | ||
// represented as a 2^n x 2^n matrix. | ||
int n; | ||
// unitary matrix representing the action of the gate | ||
std::complex<double> **matrix; | ||
|
||
public: | ||
QuantumGate (int n, std::complex<double> **matrix=NULL, bool byref=true); | ||
QuantumGate (int n, const std::complex<double> *vector); | ||
QuantumGate (const QuantumGate &orig); | ||
QuantumGate & operator= (const QuantumGate &orig); | ||
~QuantumGate (); | ||
|
||
int N () const; | ||
void set (int i, int j, std::complex<double> val); | ||
std::complex<double> operator() (int i, int j) const; | ||
QuantumGate H () const; | ||
void act (QubitSystem *q) const; | ||
void act (QubitSystem *q, int index) const; | ||
QuantumGate & operator+= (const QuantumGate &other); | ||
QuantumGate & operator-= (const QuantumGate &other); | ||
QuantumGate & operator*= (const QuantumGate &other); | ||
QuantumGate & operator^= (int e); | ||
|
||
friend void operator* (const QuantumGate &gate, QubitSystem &qubits); | ||
friend QuantumGate operator+ (const QuantumGate &g1, const QuantumGate &g2); | ||
friend QuantumGate operator- (const QuantumGate &g1, const QuantumGate &g2); | ||
friend QuantumGate operator* (const QuantumGate &g1, const QuantumGate &g2); | ||
friend QuantumGate operator^ (const QuantumGate &g1, int e); | ||
friend QuantumGate kron (const QuantumGate &g1, const QuantumGate &g2); | ||
friend QuantumGate tensor_pow (const QuantumGate &g, int e); | ||
}; | ||
|
||
|
||
} | ||
|
||
#endif | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#ifndef QUBIT_SYSTEM_H | ||
#define QUBIT_SYSTEM_H | ||
|
||
#include "quantum_gate.h" | ||
#include <complex> | ||
|
||
namespace quantum_algorithm_simulator { | ||
|
||
// Represents a system of one or more qubits. | ||
// | ||
// The number of qubits is given by N, and the coefficients on each of the | ||
// basis states are given by coeffs. For example, a 2-qubit system would be of | ||
// the form | ||
// a|00> + b|01> + c|10> + d|11>. | ||
// In this case N = 2 and coeffs would be the array {a, b, c, d}. | ||
// | ||
// Indices of bits are always one-based, but indices of states are always zero- | ||
// based. | ||
// | ||
// The indices are listed in coeffs as a vector, corresponding to the entries | ||
// of an N-dimensional array stored in row-major order. That is, | ||
// the coefficient on the basis state |b_1,b_2,b_3,...,b_N> (where each b is | ||
// 0 or 1) is given by | ||
// coeffs[N*b_1 + (N-1)*b_2 + (N-2)*b_3 + ... + 2*b_N-1 + b_N]. | ||
// Equivalently, the state whose coefficient is given by coeffs[i] is the state | ||
// whose representation in 0's and 1's is the binary representation of the | ||
// integer i: e.g., coeffs[3] will always represent the state |00...011> (where | ||
// the number of leading zeros depends on N). | ||
class QubitSystem { | ||
private: | ||
int n; // Number of qubits in the system | ||
std::complex<double> *coeffs; // Coefficients on each of the basis states | ||
|
||
QubitSystem (const QubitSystem &orig); | ||
QubitSystem & operator= (const QubitSystem &orig); | ||
int get_observed_state (); | ||
void collapse (int state); | ||
void collapse (int bit_index, int observed_value); | ||
|
||
public: | ||
QubitSystem (int n=1, int state=0); | ||
void init (int n=1, int state=0); | ||
~QubitSystem (); | ||
|
||
int N () const; | ||
int measure (); | ||
int measure (int bit_index); | ||
std::string smeasure (); | ||
int * ameasure (); | ||
|
||
friend void QuantumGate::act (QubitSystem *q) const; | ||
friend void QuantumGate::act (QubitSystem *q, int index) const; | ||
friend void operator* (const QuantumGate &gate, QubitSystem &qubits); | ||
friend void operator* (QubitSystem &q1, QubitSystem &q2); | ||
}; | ||
|
||
|
||
int bin_to_int (const std::string bin); | ||
|
||
|
||
} | ||
|
||
#endif | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
set(quantum_SRCS | ||
${quantum_SOURCE_DIR}/src/algorithms.cpp | ||
${quantum_SOURCE_DIR}/src/gate_factory.cpp | ||
${quantum_SOURCE_DIR}/src/quantum_gate.cpp | ||
${quantum_SOURCE_DIR}/src/qubit_system.cpp | ||
${quantum_SOURCE_DIR}/src/util.cpp | ||
) | ||
set(LIBRARY_OUTPUT_PATH ${quantum_SOURCE_DIR}/lib) | ||
|
||
include_directories(${quantum_SOURCE_DIR}/include) | ||
add_library(quantum SHARED ${quantum_SRCS}) | ||
|
||
|
Oops, something went wrong.