-
Notifications
You must be signed in to change notification settings - Fork 11
/
Basis.ts
245 lines (228 loc) · 7.29 KB
/
Basis.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import Complex, { Cx } from './Complex'
import Vector from './Vector'
import Operator from './Operator'
import Dimension from './Dimension'
import { INamedVector } from './interfaces'
/**
* Class for dealing with bases.
*/
export default class Basis {
namedVectors: INamedVector[]
/**
* As most constructors, for intenal use only.
* @param namedVectorValues
* @param dimension
*/
constructor(namedVectorValues: { name: string; values: Complex[] }[], computationalDimension: Dimension) {
if (namedVectorValues.length !== computationalDimension.size) {
throw new Error(
`There are ${namedVectorValues.length} vectors` +
` - incorrect for a basis for ${computationalDimension.toString()}`,
)
}
if (namedVectorValues.length !== computationalDimension.size) {
throw new Error(
`There are ${namedVectorValues.length} vectors` +
` - incorrect for a basis for ${computationalDimension.toString()}`,
)
}
this.namedVectors = namedVectorValues.map((d) => ({
name: d.name,
vector: Vector.fromArray(d.values, [computationalDimension]).normalize(),
}))
}
/**
* Get dimension.
*/
get computationalDimension(): Dimension {
return this.namedVectors[0].vector.dimensions[0]
}
get basisCoordNames(): string[] {
return this.namedVectors.map((d) => d.name)
}
/**
* Get basis string.
*/
get basisStr(): string {
return this.basisCoordNames.join('')
}
get basisDimension(): Dimension {
return this.computationalDimension.reassignCoordNames(this.basisCoordNames)
}
/**
* Generates a string.
*/
toString(): string {
const intro = `Basis ${this.basisStr} for dimension ${this.computationalDimension.name}`
const listStr = this.namedVectors.map((d) => `|${d.name}⟩ = ${d.vector.toKetString('cartesian')}`)
return `${intro}\n${listStr.join('\n')}`
}
/**
* Bases for polarization {@link Dimension.polarization}
* @param basisStr 'HV' for horizontal, 'DA' for diagonal, 'LR' for circular
*/
static polarization(basisStr: string): Basis {
switch (basisStr) {
case 'HV':
return new Basis(
[
{ name: 'H', values: [Cx(1), Cx(0)] },
{ name: 'V', values: [Cx(0), Cx(1)] },
],
Dimension.polarization(),
)
case 'DA':
return new Basis(
[
{ name: 'D', values: [Cx(1), Cx(1)] },
{ name: 'A', values: [Cx(-1), Cx(1)] },
],
Dimension.polarization(),
)
case 'LR':
return new Basis(
[
{ name: 'L', values: [Cx(1), Cx(0, 1)] },
{ name: 'R', values: [Cx(0, 1), Cx(1)] },
],
Dimension.polarization(),
)
default:
throw new Error(`Basis ${basisStr} not aviable bases for polarization ['HV', 'DA', 'LR'].`)
}
}
/**
* Bases for qubit {@link Dimension.qubit}
* @note Different from polarization, as |-⟩ ~ |0⟩ - |1⟩.
* @note |i+⟩ and |i-⟩ will test the ground for multichar coord names.
* @param basisStr '01' for computational, '+-' for diagonal, '+i-i' for circular
*/
static qubit(basisStr: string): Basis {
switch (basisStr) {
case '01':
return new Basis(
[
{ name: '0', values: [Cx(1), Cx(0)] },
{ name: '1', values: [Cx(0), Cx(1)] },
],
Dimension.qubit(),
)
case '+-':
return new Basis(
[
{ name: '+', values: [Cx(1), Cx(1)] },
{ name: '-', values: [Cx(1), Cx(-1)] },
],
Dimension.qubit(),
)
case '+i-i':
return new Basis(
[
{ name: '+i', values: [Cx(1), Cx(0, 1)] },
{ name: '-i', values: [Cx(1), Cx(0, -1)] },
],
Dimension.qubit(),
)
default:
throw new Error(`Basis ${basisStr} not aviable bases for qubits ['01', '+-', '+i-i'].`)
}
}
/**
* Bases for spin-1/2 {@link Dimension.spin}
* @note Let's test multiple names.
* @param basisStr 'ud' or 'spin-z' or 'uzdz' for z, 'spin-x' or 'uxdx', 'spin-y' or 'uydy'
*/
static spin(basisStr: string): Basis {
switch (basisStr) {
case 'spin-z':
case 'ud':
case 'uzdz':
return new Basis(
[
{ name: 'u', values: [Cx(1), Cx(0)] },
{ name: 'd', values: [Cx(0), Cx(1)] },
],
Dimension.spin(),
)
case 'spin-x':
case 'uxdx':
return new Basis(
[
{ name: 'ux', values: [Cx(1), Cx(1)] },
{ name: 'dx', values: [Cx(-1), Cx(1)] },
],
Dimension.spin(),
)
case 'spin-y':
case 'uydy':
return new Basis(
[
{ name: 'uy', values: [Cx(1), Cx(0, 1)] },
{ name: 'dy', values: [Cx(0, 1), Cx(1)] },
],
Dimension.spin(),
)
default:
throw new Error(`Basis ${basisStr} not aviable bases for spin.`)
}
}
/**
* A shorthand method for {@link Basis.polarization}, {@link Basis.spin} and {@link Basis.qubit}.
* @param dimName One of: ['polarization', 'spin', 'qubit']
* @param basisStr Basis string.
*/
static fromString(dimName: string, basisStr: string): Basis {
switch (dimName) {
case 'polarization':
return Basis.polarization(basisStr)
case 'spin':
return Basis.spin(basisStr)
case 'qubit':
return Basis.qubit(basisStr)
default:
throw new Error(`Basis.fromString: dimName ${dimName} not in ['polarization', 'spin', 'qubit'].`)
}
}
static basisChangeU(basisTo: Basis, basisFrom: Basis): Operator {
const entries = basisTo.namedVectors.flatMap((to) =>
basisFrom.namedVectors.map((from): [string[], string[], Complex] => {
return [[to.name], [from.name], to.vector.inner(from.vector)]
}),
)
return Operator.fromSparseCoordNames(entries, [basisTo.basisDimension], [basisFrom.basisDimension])
}
basisChangeUnitary(basisFrom: Basis): Operator {
return Basis.basisChangeU(this, basisFrom)
}
basisChangeUnitaryFromDimension(dimension: Dimension): Operator {
const basisStr = dimension.coordNames.join('')
switch (dimension.name) {
case 'polarization':
return this.basisChangeUnitary(Basis.polarization(basisStr))
case 'qubit':
return this.basisChangeUnitary(Basis.qubit(basisStr))
case 'spin':
return this.basisChangeUnitary(Basis.spin(basisStr))
default:
throw new Error(`Basis change not yet implemented for ${dimension}.`)
}
}
changeAllBasesUnitary(dimensions: Dimension[]): Operator {
const ops = dimensions.map((dimension) => {
if (dimension.name !== this.basisDimension.name) {
return Operator.identity([dimension])
} else {
return this.basisChangeUnitaryFromDimension(dimension)
}
})
return Operator.outer(ops)
}
changeAllDimsOfVector(vector: Vector): Vector {
return this.changeAllBasesUnitary(vector.dimensions).mulVec(vector)
}
changeAllDimsOfOperator(operator: Operator): Operator {
const changeOut = this.changeAllBasesUnitary(operator.dimensionsOut)
const changeIn = this.changeAllBasesUnitary(operator.dimensionsIn).dag()
return changeOut.mulOp(operator).mulOp(changeIn)
}
}