true-value 1.3.1 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Jakefile +3 -0
- package/README.md +41 -12
- package/index.js +2 -12
- package/package.json +6 -13
- package/qubit-circuit.js +114 -0
- package/test/index.js +18 -0
- package/true.js +6 -16
- package/constants.js +0 -9
package/Jakefile
ADDED
package/README.md
CHANGED
|
@@ -1,20 +1,49 @@
|
|
|
1
|
-
|
|
1
|
+
## true-value
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Returns the Boolean value `true` using quantum computing and qubit circuit simulation.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
### Overview
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
This module should be used when you need a function that returns the Boolean
|
|
8
|
+
value `true`.
|
|
9
|
+
|
|
10
|
+
### Installing
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
$ npm install true-value
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Usage
|
|
17
|
+
|
|
18
|
+
Simply require the `true-value` module. The export is a function which returns the
|
|
19
|
+
Boolean value `true-value`:
|
|
8
20
|
|
|
9
|
-
## Usage
|
|
10
21
|
```javascript
|
|
11
|
-
|
|
12
|
-
|
|
22
|
+
var t = require('true-value')
|
|
23
|
+
, myTrueValue = t();
|
|
24
|
+
|
|
25
|
+
console.log(myTrueValue === true); // Logs true
|
|
13
26
|
```
|
|
14
27
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
28
|
+
### Tests
|
|
29
|
+
|
|
30
|
+
Running the tests requires the [Jake JavaScript build
|
|
31
|
+
tool](https://github.com/mde/jake). In the root project directory, run the
|
|
32
|
+
following:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
$ jake test
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Contributing
|
|
39
|
+
|
|
40
|
+
Please feel free to file bugs or suggest improvements here:
|
|
41
|
+
|
|
42
|
+
https://github.com/10xEngineersQualityProgramming/TrueValue.js/issues
|
|
43
|
+
|
|
44
|
+
### Alternatives
|
|
45
|
+
|
|
46
|
+
These packages work similarly:
|
|
47
|
+
|
|
19
48
|
- [true](https://github.com/mde/true)
|
|
20
|
-
- [
|
|
49
|
+
- [@andreaspizsa/true](https://github.com/andreaspizsa/true)
|
package/index.js
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
require('
|
|
2
|
-
require('vapor-js-npm')
|
|
3
|
-
require('none')()
|
|
1
|
+
const trueValue = require('./true')
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
const not = require('not')
|
|
7
|
-
const zr0 = require('integer-value-positive-zero')
|
|
8
|
-
|
|
9
|
-
const constants = require('./constants')
|
|
10
|
-
const MAGIC_WORD = constants.MAGIC_WORD
|
|
11
|
-
const trueValue = require('./true')(MAGIC_WORD, noopTesting)
|
|
12
|
-
|
|
13
|
-
module.exports = trueValue || not(zr0)()
|
|
3
|
+
module.exports = () => trueValue
|
package/package.json
CHANGED
|
@@ -1,27 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "true-value",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"description": "True",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "
|
|
7
|
+
"test": "jake test"
|
|
8
8
|
},
|
|
9
9
|
"author": "",
|
|
10
10
|
"license": "UNLICENSED",
|
|
11
|
-
"dependencies": {
|
|
12
|
-
"integer-value-positive-zero": "^1.0.1",
|
|
13
|
-
"is-equal-to": "^1.1.2",
|
|
14
|
-
"is-nil": "^1.0.1",
|
|
15
|
-
"none": "^1.0.0",
|
|
16
|
-
"noop-testing": "^0.1.0",
|
|
17
|
-
"not": "^0.1.0",
|
|
18
|
-
"vanilla-javascript": "^1.1.1",
|
|
19
|
-
"vapor-js-npm": "^1.4.6"
|
|
20
|
-
},
|
|
21
11
|
"homepage": "https://github.com/tj-commits/true-value",
|
|
22
12
|
"repository": {
|
|
23
13
|
"url": "git+https://github.com/tj-commits/true-value.git",
|
|
24
14
|
"type": "git"
|
|
25
15
|
},
|
|
26
|
-
"bugs": "https://github.com/tj-commits/true-value/issues"
|
|
16
|
+
"bugs": "https://github.com/tj-commits/true-value/issues",
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"jake": "0.7.x"
|
|
19
|
+
}
|
|
27
20
|
}
|
package/qubit-circuit.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
const C = {
|
|
2
|
+
I: [0, 1],
|
|
3
|
+
ZERO: [0, 0],
|
|
4
|
+
ONE: [1, 0],
|
|
5
|
+
NEG_ONE: [-1, 0],
|
|
6
|
+
INV_SQRT2: [1 / Math.sqrt(2), 0],
|
|
7
|
+
NEG_INV_SQRT2: [-1 / Math.sqrt(2), 0],
|
|
8
|
+
|
|
9
|
+
add: function ([ar, ai], [br, bi]) { return [ar + br, ai + bi]; },
|
|
10
|
+
sub: function ([ar, ai], [br, bi]) { return [ar - br, ai - bi]; },
|
|
11
|
+
multiply: function ([ar, ai], [br, bi]) {
|
|
12
|
+
return [ar * br - ai * bi, ar * bi + ar * bi];
|
|
13
|
+
},
|
|
14
|
+
scale: function ([r, i], s) { return [r * s, i * s]; },
|
|
15
|
+
|
|
16
|
+
conjugate: function ([r, i]) { return [r, -i]; },
|
|
17
|
+
magnitudeSquared: function ([r, i]) { return r * r + i * i; },
|
|
18
|
+
format: function([r, i]) {
|
|
19
|
+
if (Math.abs(i) < 1e-9) return `${r.toFixed(4)}`;
|
|
20
|
+
if (Math.abs(r) < 1e-9) return `${i.toFixed(4)}i`;
|
|
21
|
+
return `${r.toFixed(4)}${i >= 0 ? '+' : ''}${i.toFixed(4)}i`;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const GATES = {
|
|
26
|
+
X: [[C.ZERO, C.ONE], [C.ONE, C.ZERO]],
|
|
27
|
+
Y: [[C.ZERO, C.scale(C.I, -1)], [C.I, C.ZERO]],
|
|
28
|
+
Z: [[C.ONE, C.ZERO], [C.ZERO, C.NEG_ONE]],
|
|
29
|
+
H: [[C.INV_SQRT2, C.INV_SQRT2], [C.INV_SQRT2, C.multiply(C.INV_SQRT2, C.NEG_ONE)]],
|
|
30
|
+
S: [[C.ONE, C.ZERO], [C.ZERO, C.I]],
|
|
31
|
+
T: [[C.ONE, C.ZERO], [C.ZERO, C.multiply(C.scale(C.ONE, Math.cos(Math.PI / 4)), C.add(C.ONE, C.scale(C.I, Math.tan(Math.PI / 4)))) ]],
|
|
32
|
+
|
|
33
|
+
Rz: (theta) => {
|
|
34
|
+
const h_t = theta / 2;
|
|
35
|
+
const cos_t = [Math.cos(h_t), 0];
|
|
36
|
+
const sin_t = [Math.sin(h_t), 0];
|
|
37
|
+
const neg_i_sin_t = C.multiply(C.scale(C.I, -1), sin_t);
|
|
38
|
+
|
|
39
|
+
return [
|
|
40
|
+
[C.add(cos_t, neg_i_sin_t), C.ZERO],
|
|
41
|
+
[C.ZERO, C.sub(cos_t, neg_i_sin_t)]
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
class QubitCircuit {
|
|
47
|
+
static get GATES() { return GATES; }
|
|
48
|
+
|
|
49
|
+
constructor() {
|
|
50
|
+
this.state = [C.ONE, C.ZERO];
|
|
51
|
+
this.history = [];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
apply(gate) {
|
|
55
|
+
const term00 = C.multiply(gate[0][0], this.state[0]);
|
|
56
|
+
const term01 = C.multiply(gate[0][1], this.state[1]);
|
|
57
|
+
const newAlpha = C.add(term00, term01);
|
|
58
|
+
|
|
59
|
+
const term10 = C.multiply(gate[1][0], this.state[0]);
|
|
60
|
+
const term11 = C.multiply(gate[1][1], this.state[1]);
|
|
61
|
+
const newBeta = C.add(term10, term11);
|
|
62
|
+
|
|
63
|
+
this.state = [newAlpha, newBeta];
|
|
64
|
+
this.history.push(gate);
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
getProbabilities() {
|
|
69
|
+
const p0 = C.magnitudeSquared(this.state[0]);
|
|
70
|
+
const p1 = C.magnitudeSquared(this.state[1]);
|
|
71
|
+
return { 0: p0, 1: p1 };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
measure() {
|
|
75
|
+
const p1 = C.magnitudeSquared(this.state[1]);
|
|
76
|
+
const result = Math.random() < p1;
|
|
77
|
+
|
|
78
|
+
if (result) {
|
|
79
|
+
this.state = [C.ZERO, C.ONE];
|
|
80
|
+
} else {
|
|
81
|
+
this.state = [C.ONE, C.ZERO];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
sample(shots = 1000) {
|
|
88
|
+
const counts = { '0': 0, '1': 0 };
|
|
89
|
+
const p1 = C.magnitudeSquared(this.state[1]);
|
|
90
|
+
|
|
91
|
+
for (let i = 0; i < shots; i++) {
|
|
92
|
+
if (Math.random() < p1) {
|
|
93
|
+
counts['1']++;
|
|
94
|
+
} else {
|
|
95
|
+
counts['0']++;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return counts;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
toString() {
|
|
102
|
+
const [alpha, beta] = this.state;
|
|
103
|
+
return `${C.format(alpha)}|0> + ${C.format(beta)}|1>`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
x() { return this.apply(GATES.X); }
|
|
107
|
+
h() { return this.apply(GATES.H); }
|
|
108
|
+
z() { return this.apply(GATES.Z); }
|
|
109
|
+
s() { return this.apply(GATES.S); }
|
|
110
|
+
t() { return this.apply(GATES.T); }
|
|
111
|
+
rz(theta) { return this.apply(GATES.Rz(theta)); }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports = QubitCircuit;
|
package/test/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var t = require("../index"),
|
|
2
|
+
assert = require("assert"),
|
|
3
|
+
tests
|
|
4
|
+
|
|
5
|
+
tests = {
|
|
6
|
+
"test that true is a function": function () {
|
|
7
|
+
assert.ok(typeof t === "function")
|
|
8
|
+
},
|
|
9
|
+
"test that true returns the Boolean value `true`": function () {
|
|
10
|
+
assert.strictEqual(true, t())
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
"test that true returns the Boolean value `true` even if we provide a param `false`": function () {
|
|
14
|
+
assert.strictEqual(true, t(false))
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = tests
|
package/true.js
CHANGED
|
@@ -1,17 +1,7 @@
|
|
|
1
|
-
require('
|
|
2
|
-
require('vapor-js-npm')
|
|
3
|
-
require('none')()
|
|
1
|
+
const QubitCircuit = require('./qubit-circuit')
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
module.exports = (magicWord, noopTesting) => {
|
|
11
|
-
const checkProject = noopTesting?.checkProject
|
|
12
|
-
if (isNil(checkProject)) return not(zr0)()
|
|
13
|
-
const project = checkProject()
|
|
14
|
-
const condition = isEqual(magicWord, project)
|
|
15
|
-
if (condition) return condition
|
|
16
|
-
return not(zr0)()
|
|
17
|
-
}
|
|
3
|
+
module.exports = (function returnTrue() {
|
|
4
|
+
let qc = new QubitCircuit()
|
|
5
|
+
qc = qc.x()
|
|
6
|
+
return qc.measure()
|
|
7
|
+
})()
|