Skip to content

Commit

Permalink
shove
Browse files Browse the repository at this point in the history
  • Loading branch information
simplygreatwork committed Jun 12, 2020
1 parent c8ad229 commit 9ba9f2e
Show file tree
Hide file tree
Showing 104 changed files with 3,632 additions and 61 deletions.
10 changes: 6 additions & 4 deletions examples/adder.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

const logger = require('../src/logger')()
const Bits = require('../src/bits')

// this example does not use quantum superposition so it's basically classical addition
// todo: create an example which adds values in super position

const Bits = require('../src/bits')

add({ a: 3, b: 12})

function add(options) {
Expand All @@ -17,7 +18,7 @@ function add(options) {
.each(function(each) {
values.a = Bits.fromArray(Bits.fromNumber(each.index, this.size).toArray().splice(5, 4)).toNumber()
values.b = Bits.fromArray(Bits.fromNumber(each.index, this.size).toArray().splice(1, 4)).toNumber()
console.log(`\n${values.a} + ${values.b} = ?\n`)
logger.log(`\n${values.a} + ${values.b} = ?\n`)
})

circuit('adding a + b into b', 10)
Expand All @@ -27,13 +28,14 @@ function add(options) {
.run('trace')
.each(function(each) {
values.result = Bits.fromArray(Bits.fromNumber(each.index, this.size).toArray().splice(1, 4)).toNumber()
console.log(`\n${values.a} + ${values.b} = ${values.result}\n`)
logger.log(`\n${values.a} + ${values.b} = ${values.result}\n`)
})
}

function circuit(name, size) {

let circuit = require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
2 changes: 2 additions & 0 deletions examples/amplify-two-states.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

const logger = require('../src/logger')()
const Bits = require('../src/bits')
const a = Bits.fromNumber(2, 4)
const b = Bits.fromNumber(6, 4)
Expand All @@ -24,6 +25,7 @@ circuit(`amplify-two-states-two-times`, 5)
function circuit(name, size, options) {

let circuit = require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
4 changes: 3 additions & 1 deletion examples/amplify-wave.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

const logger = require('../src/logger')()
const Bits = require('../src/bits')
const bits = Bits.fromNumber(3, 4)
const wave = []
Expand All @@ -12,11 +13,12 @@ circuit(`amplify-wave |${bits.toString(' x')}>`, 5)
})
}).run()

console.log('wave: ' + JSON.stringify(wave, null, 2))
logger.log('wave: ' + JSON.stringify(wave, null, 2))

function circuit(name, size, options) {

let circuit = require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
2 changes: 2 additions & 0 deletions examples/amplify.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

const logger = require('../src/logger')()
const Bits = require('../src/bits')

amplify(3)
Expand All @@ -22,6 +23,7 @@ function amplify(value) {
function circuit(name, size, options) {

let circuit = require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
3 changes: 3 additions & 0 deletions examples/bell-phase.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

circuit('bell-phase', 2)
.h(0)
.t(0)
Expand Down Expand Up @@ -37,6 +39,7 @@ circuit('bell-phase', 2)
function circuit(name, size) {

return require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
3 changes: 3 additions & 0 deletions examples/bell-state.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

circuit('bell', 2)
.h(0)
.cx(1, 0)
Expand All @@ -13,6 +15,7 @@ circuit('bell-opposite', 2)
function circuit(name, size) {

return require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
3 changes: 3 additions & 0 deletions examples/bell-tricks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

circuit('bell-phase', 2)
.h(0)
.t(0)
Expand Down Expand Up @@ -50,6 +52,7 @@ circuit('bell-phase', 2)
function circuit(name, size) {

return require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
21 changes: 11 additions & 10 deletions examples/bits.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@

const logger = require('../src/logger')()
const utility = require('../src/utility')
const Bits = require('../src/bits')

let bits = null

bits = Bits.fromNumber(12, 4)
console.log('bits.toNumber(): ' + bits.toNumber())
console.log('bits.toString("01"): ' + bits.toString('01'))
logger.log('bits.toNumber(): ' + bits.toNumber())
logger.log('bits.toString("01"): ' + bits.toString('01'))

bits = Bits.fromNumber(3, 4)
console.log('bits.toNumber(): ' + bits.toNumber())
console.log('bits.toString("01"): ' + bits.toString('01'))
logger.log('bits.toNumber(): ' + bits.toNumber())
logger.log('bits.toString("01"): ' + bits.toString('01'))

bits = Bits.fromString('0101', '01')
console.log('bits.toNumber(): ' + bits.toNumber())
console.log('bits.toString("01"): ' + bits.toString('01'))
logger.log('bits.toNumber(): ' + bits.toNumber())
logger.log('bits.toString("01"): ' + bits.toString('01'))

bits = Bits.fromString(' x x', ' x')
console.log('bits.toNumber(): ' + bits.toNumber())
console.log('bits.toString(" x"): ' + bits.toString(' x'))
console.log('bits.toString("01"): ' + bits.toString('01'))
logger.log('bits.toNumber(): ' + bits.toNumber())
logger.log('bits.toString(" x"): ' + bits.toString(' x'))
logger.log('bits.toString("01"): ' + bits.toString('01'))

bits = Bits.fromString('111000100001001', '01')
console.log('bits.toNumber(): ' + bits.toNumber())
logger.log('bits.toNumber(): ' + bits.toNumber())
3 changes: 3 additions & 0 deletions examples/entanglement.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

// once two qubits are entangled, what are some effects we can demo?
// if we rotate z on 0, will 1 also rotate on z? how can we verify this?
// how to compare and contrast entagled state vs non-entangled state?
Expand All @@ -11,6 +13,7 @@ circuit('entangle', 2)
function circuit(name, size) {

return require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
5 changes: 4 additions & 1 deletion examples/frequency-approximate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

// the frequency results here tend to be approximate
// the periods do not fit neatly into the resulting state vector; e.g. 16 / 3 = 5.3333 instead of 16 / 4 = 4
// for periods with these remainders, I need more study to determine whether to round down
Expand Down Expand Up @@ -35,12 +37,13 @@ function output(period, size) {
if (state.magnitude > result.magnitude) result = state
})
let squared = Math.pow(2, size)
console.log(`The frequency is approximately ${squared - result.index} from a period of ${period} in ${squared}.\n`)
logger.log(`The frequency is approximately ${squared - result.index} from a period of ${period} in ${squared}.\n`)
}

function Circuit(name, size, options) {

let circuit = require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
5 changes: 4 additions & 1 deletion examples/frequency.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

// This example is not yet verified for correctness; e.g. frequency = squared - state.index

frequency ({ period: 2, size: 4 })
Expand Down Expand Up @@ -29,12 +31,13 @@ function output(period, size) {
if (state.magnitude > result.magnitude) result = state
})
let squared = Math.pow(2, size)
console.log(`The frequency is ${squared - result.index} from a period of ${period} in ${squared}.\n`)
logger.log(`The frequency is ${squared - result.index} from a period of ${period} in ${squared}.\n`)
}

function Circuit(name, size, options) {

let circuit = require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
5 changes: 4 additions & 1 deletion examples/gates.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

const logger = require('../src/logger')()

circuit('circuit', 1).library(function(gates) {
console.log('library: ' + JSON.stringify(Object.keys(gates), null, 2))
logger.log('library: ' + JSON.stringify(Object.keys(gates), null, 2))
})

function circuit(name, size) {

return require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
3 changes: 3 additions & 0 deletions examples/ghz-state.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

circuit('ghz', 3)
.h(0)
.cx(1, 0)
Expand All @@ -8,6 +10,7 @@ circuit('ghz', 3)
function circuit(name, size) {

return require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
3 changes: 3 additions & 0 deletions examples/interference.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

const logger = require('../src/logger')()

circuit('interference', 4)
.run()

function circuit(name, size, options) {

return require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
29 changes: 15 additions & 14 deletions examples/math.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@

let math = require('mathjs')
const math = require('mathjs')
const logger = require('../src/logger')()

console.log()
console.log('Math.sqrt(-2): ' + Math.sqrt(-2)) // cannot find the square root of a negative number
console.log('math.sqrt(math.complex(-2)): ' + math.sqrt(math.complex(-2)))
console.log('math.sqrt(math.complex(2)): ' + math.sqrt(math.complex(2)))
console.log()
logger.log()
logger.log('Math.sqrt(-2): ' + Math.sqrt(-2)) // cannot find the square root of a negative number
logger.log('math.sqrt(math.complex(-2)): ' + math.sqrt(math.complex(-2)))
logger.log('math.sqrt(math.complex(2)): ' + math.sqrt(math.complex(2)))
logger.log()

console.log('math.pi: ', math.pi)
logger.log('math.pi: ', math.pi)
const degree = math.pi / 180
console.log('1 degree = math.pi / 180: ', degree, math.pi / 180)
console.log('360 degrees = math.pi * 2: ', 360 * degree, math.pi * 2)
console.log('180 degrees = math.pi: ', 180 * degree, math.pi)
console.log('90 degrees = math.pi / 2: ', 90 * degree, math.pi / 2)
console.log('45 degrees = math.pi / 4: ', 45 * degree, math.pi / 4)
console.log('22.5 degrees = math.pi / 8: ', 22.5 * degree, math.pi / 8)
console.log()
logger.log('1 degree = math.pi / 180: ', degree, math.pi / 180)
logger.log('360 degrees = math.pi * 2: ', 360 * degree, math.pi * 2)
logger.log('180 degrees = math.pi: ', 180 * degree, math.pi)
logger.log('90 degrees = math.pi / 2: ', 90 * degree, math.pi / 2)
logger.log('45 degrees = math.pi / 4: ', 45 * degree, math.pi / 4)
logger.log('22.5 degrees = math.pi / 8: ', 22.5 * degree, math.pi / 8)
logger.log()
2 changes: 2 additions & 0 deletions examples/measure-repeatedly.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

const logger = require('../src/logger')()

// illustrate probability recording with multiple runs
// because internal circuit probabilities can never really be known
2 changes: 2 additions & 0 deletions examples/measure.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

// illustrate through measuring why it is necessary to run a circuit perhaps hundreds of times
// to get a glimpse of the real certainty of an answer
// measuring only gives reveals the state vector combination - not probabilities
3 changes: 3 additions & 0 deletions examples/not-conditionally-toffoli-constructed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

circuit('ccx-built-in-matrix', 3)
.x(0)
.x(1)
Expand All @@ -14,6 +16,7 @@ circuit('ccx-constructed-toffoli', 3)
function circuit(name, size, options) {

let circuit = require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
3 changes: 3 additions & 0 deletions examples/not-conditionally-with-one-control.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

circuit('cx', 2)
.cx(1, 0)
.run('trace', 'changed')
Expand All @@ -17,6 +19,7 @@ circuit('x x cx', 2)
function circuit(name, size) {

return require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
3 changes: 3 additions & 0 deletions examples/not-conditionally-with-two-controls.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

circuit('ccx false', 3)
.x(0)
.ccx(2, [0, 1])
Expand All @@ -13,6 +15,7 @@ circuit('ccx true', 3)
function circuit(name, size) {

return require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
3 changes: 3 additions & 0 deletions examples/not.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const logger = require('../src/logger')()

circuit('setting the first bit (0) changes the rightmost bit below', 10)
.x(0)
.run('trace')
Expand All @@ -10,6 +12,7 @@ circuit('setting the last bit (0) changes the leftmost bit below', 10)
function circuit(name, size) {

return require('../src/circuit.js')(name, size, {
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
Expand Down
Loading

0 comments on commit 9ba9f2e

Please sign in to comment.