@quantumplayed/quantum-game-engine 1.0.2 → 1.2.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.
Files changed (52) hide show
  1. package/README.md +179 -0
  2. package/dist/EntanglementEngine.d.ts +8 -44
  3. package/dist/EntanglementEngine.js +7 -487
  4. package/dist/EntanglementEngine.js.map +1 -1
  5. package/dist/Operations.d.ts +6 -0
  6. package/dist/Operations.js +48 -0
  7. package/dist/Operations.js.map +1 -1
  8. package/dist/QuantumEntity.d.ts +18 -11
  9. package/dist/QuantumEntity.js +47 -101
  10. package/dist/QuantumEntity.js.map +1 -1
  11. package/dist/QuantumSystem.d.ts +64 -0
  12. package/dist/QuantumSystem.js +510 -0
  13. package/dist/QuantumSystem.js.map +1 -0
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.js +1 -0
  16. package/dist/index.js.map +1 -1
  17. package/package.json +2 -2
  18. package/dist/src/CircuitLog.d.ts +0 -54
  19. package/dist/src/CircuitLog.js +0 -80
  20. package/dist/src/CircuitLog.js.map +0 -1
  21. package/dist/src/Complex.d.ts +0 -13
  22. package/dist/src/Complex.d.ts.map +0 -1
  23. package/dist/src/Complex.js +0 -49
  24. package/dist/src/Complex.js.map +0 -1
  25. package/dist/src/EntanglementEngine.d.ts +0 -48
  26. package/dist/src/EntanglementEngine.d.ts.map +0 -1
  27. package/dist/src/EntanglementEngine.js +0 -456
  28. package/dist/src/EntanglementEngine.js.map +0 -1
  29. package/dist/src/Operations.d.ts +0 -18
  30. package/dist/src/Operations.d.ts.map +0 -1
  31. package/dist/src/Operations.js +0 -107
  32. package/dist/src/Operations.js.map +0 -1
  33. package/dist/src/QuantumEntity.d.ts +0 -26
  34. package/dist/src/QuantumEntity.d.ts.map +0 -1
  35. package/dist/src/QuantumEntity.js +0 -142
  36. package/dist/src/QuantumEntity.js.map +0 -1
  37. package/dist/src/index.d.ts +0 -5
  38. package/dist/src/index.d.ts.map +0 -1
  39. package/dist/src/index.js +0 -6
  40. package/dist/src/index.js.map +0 -1
  41. package/dist/tests/DensityMatrix.test.d.ts +0 -1
  42. package/dist/tests/DensityMatrix.test.d.ts.map +0 -1
  43. package/dist/tests/DensityMatrix.test.js +0 -41
  44. package/dist/tests/DensityMatrix.test.js.map +0 -1
  45. package/dist/tests/EntanglementEngine.test.d.ts +0 -1
  46. package/dist/tests/EntanglementEngine.test.d.ts.map +0 -1
  47. package/dist/tests/EntanglementEngine.test.js +0 -72
  48. package/dist/tests/EntanglementEngine.test.js.map +0 -1
  49. package/dist/tests/QuantumEntity.test.d.ts +0 -1
  50. package/dist/tests/QuantumEntity.test.d.ts.map +0 -1
  51. package/dist/tests/QuantumEntity.test.js +0 -60
  52. package/dist/tests/QuantumEntity.test.js.map +0 -1
package/README.md CHANGED
@@ -312,3 +312,182 @@ EntanglementEngine.conditionalInteract({
312
312
  // Because everything is sparse, branches where mathematically invalid things happen
313
313
  // (like trying to capture an empty square) don't consume memory.
314
314
  ```
315
+
316
+ ---
317
+
318
+ ## 6. Higher-Level Board Game Abstractions (The BoardGameHelpers Pattern)
319
+
320
+ For board games (like Checkers or Chess), you can wrap low-level transitions and complex amplitudes into an intuitive, custom helper class within your project. This keeps your game rules code clean and accessible to non-quantum experts.
321
+
322
+ Here is the recommended pattern:
323
+
324
+ ```typescript
325
+ import { QuantumEntity, EntanglementEngine, Operations } from '@quantumplayed/quantum-game-engine';
326
+
327
+ export class BoardGameHelpers {
328
+ /**
329
+ * Move a piece classically from source to target.
330
+ * Transitions state |1, 0> (source occupied, target empty) to |0, 1>.
331
+ */
332
+ static move(source: QuantumEntity, target: QuantumEntity): void {
333
+ const trans = [{ from: 2, to: 1, amplitude: { re: 1, im: 0 } }];
334
+ EntanglementEngine.interactMany([source, target], Operations.SparseTransition(trans));
335
+ }
336
+
337
+ /**
338
+ * Splits a piece equally into a superposition across two target entities.
339
+ * Ensure targets are cleared to state 0 (empty) first before calling.
340
+ */
341
+ static split(source: QuantumEntity, target1: QuantumEntity, target2: QuantumEntity): void {
342
+ target1.amplitudes = { 0: { re: 1, im: 0 } };
343
+ target1.entangledSystem = undefined;
344
+ target2.amplitudes = { 0: { re: 1, im: 0 } };
345
+ target2.entangledSystem = undefined;
346
+
347
+ const trans = [
348
+ { from: 4, to: 2, amplitude: { re: 0.70710678, im: 0 } }, // |1,0,0> -> |0,1,0>
349
+ { from: 4, to: 1, amplitude: { re: 0.70710678, im: 0 } } // |1,0,0> -> |0,0,1>
350
+ ];
351
+ EntanglementEngine.interactMany([source, target1, target2], Operations.SparseTransition(trans));
352
+ }
353
+
354
+ /**
355
+ * Merges a split piece from two source entities back into a single target entity.
356
+ * Handles positive/negative phases for constructive/destructive interference.
357
+ */
358
+ static merge(source1: QuantumEntity, source2: QuantumEntity, target: QuantumEntity): void {
359
+ const trans = [
360
+ { from: 4, to: 1, amplitude: { re: 0.70710678, im: 0 } }, // |1,0,0> -> |0,0,1>
361
+ { from: 4, to: 6, amplitude: { re: 0.70710678, im: 0 } }, // |1,0,0> -> |1,1,0>
362
+ { from: 2, to: 1, amplitude: { re: 0.70710678, im: 0 } }, // |0,1,0> -> |0,0,1>
363
+ { from: 2, to: 6, amplitude: { re: -0.70710678, im: 0 } } // |0,1,0> -> -|1,1,0>
364
+ ];
365
+ EntanglementEngine.interactMany([source1, source2, target], Operations.SparseTransition(trans));
366
+ }
367
+
368
+ /**
369
+ * Performs a conditional capture (Toffoli gate logic): if both attacker and victim are occupied,
370
+ * the attacker moves to target and victim is captured (cleared to empty).
371
+ */
372
+ static capture(attacker: QuantumEntity, victim: QuantumEntity, target: QuantumEntity): void {
373
+ EntanglementEngine.conditionalInteract({
374
+ controls: [
375
+ { entity: attacker, state: 1 },
376
+ { entity: victim, state: 1 }
377
+ ],
378
+ targets: [
379
+ { entity: attacker, operation: Operations.Shift(1) },
380
+ { entity: target, operation: Operations.Shift(1) },
381
+ { entity: victim, operation: Operations.Shift(1) }
382
+ ]
383
+ });
384
+ }
385
+
386
+ /**
387
+ * Performs conditional captures for multiple victims sequentially.
388
+ */
389
+ static captureMany(attacker: QuantumEntity, victims: QuantumEntity[], target: QuantumEntity): void {
390
+ for (const victim of victims) {
391
+ this.capture(attacker, victim, target);
392
+ }
393
+ }
394
+ }
395
+ ```
396
+
397
+ ---
398
+
399
+ ## 7. Quantum Card Abstractions (The CardGameHelpers Pattern)
400
+
401
+ For quantum card games (like Quantum Poker or Blackjack), cards can exist in a superposition of multiple ranks/suits until they are observed.
402
+
403
+ Here is the helper pattern:
404
+
405
+ ```typescript
406
+ import { QuantumEntity, Operations, EntanglementEngine } from '@quantumplayed/quantum-game-engine';
407
+
408
+ export class CardGameHelpers {
409
+ /**
410
+ * Shuffles a card qudit (dimension = number of ranks in deck)
411
+ * into a uniform superposition of possible ranks.
412
+ */
413
+ static shuffleSuperposition(card: QuantumEntity, fromRank: number, possibleRanks: number[]): void {
414
+ const factor = 1 / Math.sqrt(possibleRanks.length);
415
+ const transitions = possibleRanks.map(rank => ({
416
+ from: fromRank,
417
+ to: rank,
418
+ amplitude: { re: factor, im: 0 }
419
+ }));
420
+ card.apply(Operations.SparseTransition(transitions));
421
+ }
422
+
423
+ /**
424
+ * Deals a card from the deck to a player hand slot, entangling them.
425
+ * Transitions card from |deck_occupied, hand_empty> to |deck_empty, hand_occupied>.
426
+ */
427
+ static dealCard(deckSlot: QuantumEntity, handSlot: QuantumEntity): void {
428
+ // Assume 2-dimensional qudits (0=Empty/Card Gone, 1=Card Present)
429
+ const trans = [{ from: 2, to: 1, amplitude: { re: 1, im: 0 } }]; // |1,0> -> |0,1>
430
+ EntanglementEngine.interactMany([deckSlot, handSlot], Operations.SparseTransition(trans));
431
+ }
432
+ }
433
+ ```
434
+
435
+ ---
436
+
437
+ ## 8. Quantum Inventory & Item Systems (The InventoryHelpers Pattern)
438
+
439
+ In adventure games or RPGs, you can entangle chest contents or puzzles with player inventory state. If a player obtains a key in a superposition branch, the chest status becomes entangled with that reality branch.
440
+
441
+ Here is the helper pattern:
442
+
443
+ ```typescript
444
+ import { QuantumEntity, Operations, EntanglementEngine } from '@quantumplayed/quantum-game-engine';
445
+
446
+ export class InventoryHelpers {
447
+ /**
448
+ * Entangles a chest and its reward with a key in the player's inventory.
449
+ * IF the key is gathered (state 1) and chest is closed (state 0):
450
+ * 1. Chest transitions to Open (state 1)
451
+ * 2. Reward item transitions to Obtained (state 1)
452
+ */
453
+ static entangleReward(key: QuantumEntity, chest: QuantumEntity, rewardItem: QuantumEntity): void {
454
+ EntanglementEngine.conditionalInteract({
455
+ controls: [
456
+ { entity: key, state: 1 },
457
+ { entity: chest, state: 0 }
458
+ ],
459
+ targets: [
460
+ { entity: chest, operation: Operations.Shift(1) }, // 0 (Closed) -> 1 (Open)
461
+ { entity: rewardItem, operation: Operations.Shift(1) } // 0 (None) -> 1 (Obtained)
462
+ ]
463
+ });
464
+ }
465
+ }
466
+ ```
467
+
468
+ ---
469
+
470
+ ## 9. Branching Quantum Narrative Trees (The NarrativeHelpers Pattern)
471
+
472
+ In branching dialogue systems or RPG narrative questlines, the story can branch down multiple paths simultaneously. The state of the world remains in a superposition until a player observes a dialogue consequence.
473
+
474
+ Here is the helper pattern:
475
+
476
+ ```typescript
477
+ import { QuantumEntity, Operations } from '@quantumplayed/quantum-game-engine';
478
+
479
+ export class NarrativeHelpers {
480
+ /**
481
+ * Splits the narrative state (dimension = number of quest state nodes)
482
+ * from a root dialog node into an equal superposition of two branching outcomes.
483
+ */
484
+ static splitDecision(narrativeState: QuantumEntity, fromNode: number, branchANode: number, branchBNode: number): void {
485
+ const trans = [
486
+ { from: fromNode, to: branchANode, amplitude: { re: 0.70710678, im: 0 } },
487
+ { from: fromNode, to: branchBNode, amplitude: { re: 0.70710678, im: 0 } }
488
+ ];
489
+ narrativeState.apply(Operations.SparseTransition(trans));
490
+ }
491
+ }
492
+ ```
493
+
@@ -1,50 +1,14 @@
1
- import { QuantumEntity, type Predicate } from './QuantumEntity.js';
2
- import { type QuantumOperation, type SparseAmplitudes } from './Operations.js';
3
- import { type Complex } from './Complex.js';
4
- export declare class JointQuantumSystem {
5
- static maxDimensionLimit: number;
6
- static setMaxDimensionLimit(limit: number): void;
7
- entities: QuantumEntity[];
8
- dimensions: number[];
9
- totalDimension: number;
10
- jointAmplitudes: SparseAmplitudes;
11
- private constructor();
12
- static create(entities: QuantumEntity[]): JointQuantumSystem;
13
- static merge(involvedEntities: QuantumEntity[]): JointQuantumSystem;
14
- private _calculateTensorProduct;
15
- decomposeState(jointState: number): number[];
16
- composeState(states: number[]): number;
17
- applyLocal(entityIndex: number, operation: QuantumOperation): void;
18
- applyToSubspace(targetEntities: QuantumEntity[], operation: QuantumOperation): void;
19
- getTargetProbabilities(targetEntity: QuantumEntity): Record<number, number>;
20
- getTargetAmplitudes(targetEntity: QuantumEntity): Record<number, Complex>;
21
- measureTarget(targetEntity: QuantumEntity, forcedOutcome?: number): number;
22
- getProjectedAmplitudes(conditions: {
23
- entity: QuantumEntity;
24
- state: number;
25
- }[]): SparseAmplitudes;
26
- getJointProbability(conditions: {
27
- entity: QuantumEntity;
28
- state: number;
29
- }[]): number;
30
- areMutuallyExclusive(conditions: {
31
- entity: QuantumEntity;
32
- state: number;
33
- }[]): boolean;
34
- getReducedDensityMatrix(targetEntity: QuantumEntity): Record<number, Record<number, Complex>>;
35
- /**
36
- * Create a joint system from existing amplitudes and entities (Bit-Perfect Restoration/Cloning).
37
- */
38
- static inject(entities: QuantumEntity[], jointAmplitudes: SparseAmplitudes): JointQuantumSystem;
39
- }
40
- export interface TargetOperation {
41
- entity: QuantumEntity;
42
- operation: QuantumOperation;
43
- }
1
+ import { QuantumEntity } from './QuantumEntity.js';
2
+ import { QuantumSystem, type Predicate, type TargetOperation } from './QuantumSystem.js';
3
+ import { type QuantumOperation } from './Operations.js';
4
+ export { QuantumSystem as JointQuantumSystem, type TargetOperation, type Predicate };
5
+ /**
6
+ * @deprecated Use QuantumSystem.interact or QuantumSystem.conditionalInteract directly.
7
+ */
44
8
  export declare const EntanglementEngine: {
45
9
  interactMany(entities: QuantumEntity[], operation: QuantumOperation): void;
46
10
  conditionalInteract(args: {
47
11
  controls: Predicate[];
48
12
  targets: TargetOperation[];
49
- }): void;
13
+ }): QuantumSystem;
50
14
  };