true-value 2.0.0 → 3.0.0
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/README.md +10 -51
- package/index.d.ts +3 -0
- package/index.js +2 -2
- package/package.json +4 -3
- package/qubit-circuit.js +148 -100
- package/true.js +4 -3
- package/Jakefile +0 -3
- package/bin/cli.js +0 -15
- package/test/index.js +0 -18
package/README.md
CHANGED
|
@@ -1,59 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
# true-value
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
### Overview
|
|
6
|
-
|
|
7
|
-
This module should be used when you need a function that returns the Boolean
|
|
8
|
-
value `true`.
|
|
9
|
-
|
|
10
|
-
### Installing
|
|
3
|
+
Returns the boolean value true using quantum computing/qubit circuit simulation
|
|
11
4
|
|
|
5
|
+
## Installation
|
|
12
6
|
```bash
|
|
13
|
-
|
|
7
|
+
npm install true-value
|
|
14
8
|
```
|
|
15
9
|
|
|
16
|
-
|
|
10
|
+
## Usage
|
|
11
|
+
```js
|
|
12
|
+
const trueValue = require("true-value")
|
|
17
13
|
|
|
18
|
-
|
|
19
|
-
Boolean value `true`:
|
|
20
|
-
|
|
21
|
-
```javascript
|
|
22
|
-
var t = require('./true')
|
|
23
|
-
, myTrueValue = t();
|
|
24
|
-
|
|
25
|
-
console.log(myTrueValue === true); // Logs 'true'
|
|
14
|
+
console.log(trueValue()) // true
|
|
26
15
|
```
|
|
27
16
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
If you install `true` globally, you can call it appropriately as a CLI command:
|
|
31
|
-
|
|
32
|
-
```bash
|
|
33
|
-
$ npm install true -g
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
You will likely have to use `sudo` to install it globally.
|
|
37
|
-
|
|
38
|
-
### Tests
|
|
39
|
-
|
|
40
|
-
Running the tests requires the [Jake JavaScript build
|
|
41
|
-
tool](https://github.com/mde/jake). In the root project directory, run the
|
|
42
|
-
following:
|
|
43
|
-
|
|
44
|
-
```bash
|
|
45
|
-
$ jake test
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### Contributing
|
|
49
|
-
|
|
50
|
-
Please feel free to file bugs or suggest improvements here:
|
|
51
|
-
|
|
52
|
-
https://github.com/10xEngineersQualityProgramming/TrueValue.js/issues
|
|
53
|
-
|
|
54
|
-
### Alternatives
|
|
55
|
-
|
|
56
|
-
These packages work similarly:
|
|
57
|
-
|
|
58
|
-
- [true](https://github.com/mde/true)
|
|
59
|
-
- [@andreaspizsa/true](https://github.com/andreaspizsa/true)
|
|
17
|
+
## Contributing
|
|
18
|
+
please contribute.
|
package/index.d.ts
ADDED
package/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
const trueValue = require(
|
|
1
|
+
const trueValue = require("./true")
|
|
2
2
|
|
|
3
|
-
module.exports = ()
|
|
3
|
+
module.exports = require("literally")(trueValue)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "true-value",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "True",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"type": "git"
|
|
15
15
|
},
|
|
16
16
|
"bugs": "https://github.com/tj-commits/true-value/issues",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"construct-new-second": "^1.0.0",
|
|
19
|
+
"literally": "^1.0.0"
|
|
19
20
|
}
|
|
20
21
|
}
|
package/qubit-circuit.js
CHANGED
|
@@ -1,114 +1,162 @@
|
|
|
1
1
|
const C = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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]) {
|
|
10
|
+
return [ar + br, ai + bi]
|
|
11
|
+
},
|
|
12
|
+
sub: function ([ar, ai], [br, bi]) {
|
|
13
|
+
return [ar - br, ai - bi]
|
|
14
|
+
},
|
|
15
|
+
multiply: function ([ar, ai], [br, bi]) {
|
|
16
|
+
return [ar * br - ai * bi, ar * bi + ar * bi]
|
|
17
|
+
},
|
|
18
|
+
scale: function ([r, i], s) {
|
|
19
|
+
return [r * s, i * s]
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
conjugate: function ([r, i]) {
|
|
23
|
+
return [r, -i]
|
|
24
|
+
},
|
|
25
|
+
magnitudeSquared: function ([r, i]) {
|
|
26
|
+
return r * r + i * i
|
|
27
|
+
},
|
|
28
|
+
format: function ([r, i]) {
|
|
29
|
+
if (Math.abs(i) < 1e-9) return `${r.toFixed(4)}`
|
|
30
|
+
if (Math.abs(r) < 1e-9) return `${i.toFixed(4)}i`
|
|
31
|
+
return `${r.toFixed(4)}${i >= 0 ? "+" : ""}${i.toFixed(4)}i`
|
|
32
|
+
},
|
|
33
|
+
}
|
|
24
34
|
|
|
25
35
|
const GATES = {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
36
|
+
X: [
|
|
37
|
+
[C.ZERO, C.ONE],
|
|
38
|
+
[C.ONE, C.ZERO],
|
|
39
|
+
],
|
|
40
|
+
Y: [
|
|
41
|
+
[C.ZERO, C.scale(C.I, -1)],
|
|
42
|
+
[C.I, C.ZERO],
|
|
43
|
+
],
|
|
44
|
+
Z: [
|
|
45
|
+
[C.ONE, C.ZERO],
|
|
46
|
+
[C.ZERO, C.NEG_ONE],
|
|
47
|
+
],
|
|
48
|
+
H: [
|
|
49
|
+
[C.INV_SQRT2, C.INV_SQRT2],
|
|
50
|
+
[C.INV_SQRT2, C.multiply(C.INV_SQRT2, C.NEG_ONE)],
|
|
51
|
+
],
|
|
52
|
+
S: [
|
|
53
|
+
[C.ONE, C.ZERO],
|
|
54
|
+
[C.ZERO, C.I],
|
|
55
|
+
],
|
|
56
|
+
T: [
|
|
57
|
+
[C.ONE, C.ZERO],
|
|
58
|
+
[
|
|
59
|
+
C.ZERO,
|
|
60
|
+
C.multiply(
|
|
61
|
+
C.scale(C.ONE, Math.cos(Math.PI / 4)),
|
|
62
|
+
C.add(C.ONE, C.scale(C.I, Math.tan(Math.PI / 4)))
|
|
63
|
+
),
|
|
64
|
+
],
|
|
65
|
+
],
|
|
66
|
+
|
|
67
|
+
Rz: (theta) => {
|
|
68
|
+
const h_t = theta / 2
|
|
69
|
+
const cos_t = [Math.cos(h_t), 0]
|
|
70
|
+
const sin_t = [Math.sin(h_t), 0]
|
|
71
|
+
const neg_i_sin_t = C.multiply(C.scale(C.I, -1), sin_t)
|
|
72
|
+
|
|
73
|
+
return [
|
|
74
|
+
[C.add(cos_t, neg_i_sin_t), C.ZERO],
|
|
75
|
+
[C.ZERO, C.sub(cos_t, neg_i_sin_t)],
|
|
76
|
+
]
|
|
77
|
+
},
|
|
78
|
+
}
|
|
45
79
|
|
|
46
80
|
class QubitCircuit {
|
|
47
|
-
|
|
81
|
+
static get GATES() {
|
|
82
|
+
return GATES
|
|
83
|
+
}
|
|
48
84
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
85
|
+
constructor() {
|
|
86
|
+
this.state = [C.ONE, C.ZERO]
|
|
87
|
+
this.history = []
|
|
88
|
+
}
|
|
53
89
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
90
|
+
apply(gate) {
|
|
91
|
+
const term00 = C.multiply(gate[0][0], this.state[0])
|
|
92
|
+
const term01 = C.multiply(gate[0][1], this.state[1])
|
|
93
|
+
const newAlpha = C.add(term00, term01)
|
|
58
94
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
95
|
+
const term10 = C.multiply(gate[1][0], this.state[0])
|
|
96
|
+
const term11 = C.multiply(gate[1][1], this.state[1])
|
|
97
|
+
const newBeta = C.add(term10, term11)
|
|
62
98
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
}
|
|
99
|
+
this.state = [newAlpha, newBeta]
|
|
100
|
+
this.history.push(gate)
|
|
101
|
+
return this
|
|
102
|
+
}
|
|
73
103
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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;
|
|
104
|
+
getProbabilities() {
|
|
105
|
+
const p0 = C.magnitudeSquared(this.state[0])
|
|
106
|
+
const p1 = C.magnitudeSquared(this.state[1])
|
|
107
|
+
return { 0: p0, 1: p1 }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
measure() {
|
|
111
|
+
const p1 = C.magnitudeSquared(this.state[1])
|
|
112
|
+
const result = Math.random() < p1
|
|
113
|
+
|
|
114
|
+
if (result) {
|
|
115
|
+
this.state = [C.ZERO, C.ONE]
|
|
116
|
+
} else {
|
|
117
|
+
this.state = [C.ONE, C.ZERO]
|
|
99
118
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
119
|
+
|
|
120
|
+
return result
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
sample(shots = 1000) {
|
|
124
|
+
const counts = { 0: 0, 1: 0 }
|
|
125
|
+
const p1 = C.magnitudeSquared(this.state[1])
|
|
126
|
+
|
|
127
|
+
for (let i = 0; i < shots; i++) {
|
|
128
|
+
if (Math.random() < p1) {
|
|
129
|
+
counts["1"]++
|
|
130
|
+
} else {
|
|
131
|
+
counts["0"]++
|
|
132
|
+
}
|
|
104
133
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
134
|
+
return counts
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
toString() {
|
|
138
|
+
const [alpha, beta] = this.state
|
|
139
|
+
return `${C.format(alpha)}|0> + ${C.format(beta)}|1>`
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
x() {
|
|
143
|
+
return this.apply(GATES.X)
|
|
144
|
+
}
|
|
145
|
+
h() {
|
|
146
|
+
return this.apply(GATES.H)
|
|
147
|
+
}
|
|
148
|
+
z() {
|
|
149
|
+
return this.apply(GATES.Z)
|
|
150
|
+
}
|
|
151
|
+
s() {
|
|
152
|
+
return this.apply(GATES.S)
|
|
153
|
+
}
|
|
154
|
+
t() {
|
|
155
|
+
return this.apply(GATES.T)
|
|
156
|
+
}
|
|
157
|
+
rz(theta) {
|
|
158
|
+
return this.apply(GATES.Rz(theta))
|
|
159
|
+
}
|
|
112
160
|
}
|
|
113
161
|
|
|
114
|
-
module.exports = QubitCircuit
|
|
162
|
+
module.exports = QubitCircuit
|
package/true.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
const QubitCircuit = require(
|
|
1
|
+
const QubitCircuit = require("./qubit-circuit")
|
|
2
|
+
const construct = require("construct-new-second")
|
|
2
3
|
|
|
3
4
|
module.exports = (function returnTrue() {
|
|
4
|
-
let qc =
|
|
5
|
+
let qc = construct(QubitCircuit)
|
|
5
6
|
qc = qc.x()
|
|
6
7
|
return qc.measure()
|
|
7
|
-
})()
|
|
8
|
+
})()
|
package/Jakefile
DELETED
package/bin/cli.js
DELETED
package/test/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
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
|