|
1 | 1 | package org.quisl.framework.java.registers.classical;
|
2 | 2 |
|
3 |
| -import org.quisl.framework.java.math.complex.ComplexNumber; |
4 | 3 | import org.quisl.framework.java.registers.Register;
|
| 4 | +import org.quisl.framework.java.registers.common.RegisterUnitPrefixes; |
5 | 5 | import org.quisl.framework.java.units.computing.classical.binary.deterministic.bits.Bit;
|
6 |
| -import org.quisl.framework.java.units.computing.quantum.binary.qubits.Qubit; |
7 | 6 |
|
8 |
| -import java.util.Arrays; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Objects; |
9 | 10 |
|
10 | 11 | public class ClassicalRegister extends Register {
|
11 | 12 |
|
12 |
| - private Bit[] bits; |
| 13 | + private List<Bit> bits; |
13 | 14 |
|
14 |
| - private float[] bitsProbabilities; |
15 | 15 |
|
| 16 | + public ClassicalRegister(Long id, Integer numBits) { |
16 | 17 |
|
17 |
| - public ClassicalRegister(Long id) { |
| 18 | + super( id, RegisterUnitPrefixes.CLASSICAL_REGISTER_UNIT.getRegisterUnitCharacter(), |
| 19 | + ( "cl-reg-" + id ), numBits ); |
18 | 20 |
|
19 |
| - super(id, "cl_reg" + id); |
| 21 | + this.bits = new ArrayList<>(numBits); |
20 | 22 |
|
21 |
| - this.setup("|0⟩", "|1⟩"); |
| 23 | + this.addBits(numBits); |
22 | 24 |
|
23 | 25 | }
|
24 | 26 |
|
25 |
| - public ClassicalRegister(Long id, String stateNameBitNum1, String stateNameBitNum2) { |
| 27 | + public ClassicalRegister(Long id, String classicalRegisterName, Integer numBits) { |
26 | 28 |
|
27 |
| - super(id, "cl_reg" + id); |
| 29 | + super( id, RegisterUnitPrefixes.CLASSICAL_REGISTER_UNIT.getRegisterUnitCharacter(), |
| 30 | + classicalRegisterName, numBits ); |
28 | 31 |
|
29 |
| - this.setup(stateNameBitNum1, stateNameBitNum2); |
| 32 | + this.bits = new ArrayList<>(numBits); |
30 | 33 |
|
31 |
| - } |
32 |
| - |
33 |
| - public ClassicalRegister(Long id, String classicalRegisterName, String stateNameBitNum1, String stateNameBitNum2) { |
34 |
| - |
35 |
| - super(id, classicalRegisterName); |
36 |
| - |
37 |
| - this.setup(stateNameBitNum1, stateNameBitNum2); |
| 34 | + this.addBits(numBits); |
38 | 35 |
|
39 | 36 | }
|
40 | 37 |
|
41 |
| - private void setup(String stateNameBitNum1, String stateNameBitNum2) { |
| 38 | + public void addBits(Integer numBits) { |
42 | 39 |
|
43 |
| - this.setupBits(stateNameBitNum1, stateNameBitNum2); |
44 |
| - this.setupBitsProbabilities(); |
| 40 | + for(Long currentBit = 0L; currentBit < numBits; currentBit++) { |
45 | 41 |
|
46 |
| - } |
| 42 | + Bit bit = new Bit(currentBit); |
47 | 43 |
|
48 |
| - private void setupBits(String stateNameBitNum1, String stateNameBitNum2) { |
| 44 | + this.bits.add(bit); |
49 | 45 |
|
50 |
| - this.bits = new Bit[2]; |
51 |
| - |
52 |
| - this.bits[0] = new Bit(1L, Bit.ZERO_STATE, stateNameBitNum1); |
53 |
| - this.bits[1] = new Bit(2L, Bit.ONE_STATE, stateNameBitNum2); |
| 46 | + } |
54 | 47 |
|
55 | 48 | }
|
56 | 49 |
|
57 |
| - private void setupBitsProbabilities() { |
58 |
| - |
59 |
| - this.bitsProbabilities = new float[2]; |
60 |
| - |
61 |
| - this.bitsProbabilities[Bit.ZERO_STATE] = 1.0f; |
62 |
| - this.bitsProbabilities[Bit.ONE_STATE] = 0.0f; |
| 50 | + public List<Bit> getBits() { |
63 | 51 |
|
64 |
| - } |
65 |
| - |
66 |
| - public Bit[] getBits() { |
67 | 52 | return this.bits;
|
68 |
| - } |
69 |
| - |
70 |
| - public void setBits(Bit[] bits) { |
71 |
| - this.bits = bits; |
72 |
| - } |
73 |
| - |
74 |
| - public float[] getBitsProbabilities() { |
75 |
| - return this.bitsProbabilities; |
76 |
| - } |
77 | 53 |
|
78 |
| - public void setBitsProbabilities(float[] bitsProbabilities) { |
79 |
| - this.bitsProbabilities = bitsProbabilities; |
80 | 54 | }
|
81 | 55 |
|
82 |
| - public void retrieveMeasurement(Qubit qubit) { |
| 56 | + public void setBits(List<Bit> bits) { |
83 | 57 |
|
84 |
| - if ( qubit.isMeasurementPerformed() ) { |
85 |
| - |
86 |
| - ComplexNumber[] qubitAmplitudes = qubit.getAmplitudes(); |
87 |
| - |
88 |
| - this.bitsProbabilities[0] = (float) Math.pow(qubitAmplitudes[0].modulus(), 2.0); |
89 |
| - this.bitsProbabilities[1] = (float) Math.pow(qubitAmplitudes[1].modulus(), 2.0); |
90 |
| - |
91 |
| - } |
| 58 | + this.bits = bits; |
92 | 59 |
|
93 | 60 | }
|
94 | 61 |
|
95 | 62 | @Override
|
96 | 63 | public boolean equals(Object o) {
|
97 | 64 |
|
98 | 65 | if (this == o) {
|
| 66 | + |
99 | 67 | return true;
|
| 68 | + |
100 | 69 | }
|
101 | 70 |
|
102 | 71 | if (o == null || getClass() != o.getClass()) {
|
| 72 | + |
103 | 73 | return false;
|
| 74 | + |
104 | 75 | }
|
105 | 76 |
|
106 | 77 | if (!super.equals(o)) {
|
| 78 | + |
107 | 79 | return false;
|
| 80 | + |
108 | 81 | }
|
109 | 82 |
|
110 | 83 | ClassicalRegister that = (ClassicalRegister) o;
|
111 | 84 |
|
112 |
| - return Arrays.equals(bits, that.bits) && |
113 |
| - Arrays.equals(bitsProbabilities, that.bitsProbabilities); |
| 85 | + return Objects.equals(this.bits, that.bits); |
114 | 86 |
|
115 | 87 | }
|
116 | 88 |
|
117 | 89 | @Override
|
118 | 90 | public int hashCode() {
|
119 | 91 |
|
120 |
| - int result = super.hashCode(); |
121 |
| - |
122 |
| - result = 31 * result + Arrays.hashCode(bits); |
123 |
| - |
124 |
| - result = 31 * result + Arrays.hashCode(bitsProbabilities); |
125 |
| - |
126 |
| - return result; |
| 92 | + return Objects.hash(super.hashCode(), this.bits); |
127 | 93 |
|
128 | 94 | }
|
129 | 95 |
|
130 |
| - @Override |
131 |
| - public String toString() { |
132 |
| - return "ClassicalRegister{" + |
133 |
| - "bits=" + Arrays.toString(bits) + |
134 |
| - ", bitsProbabilities=" + Arrays.toString(bitsProbabilities) + |
135 |
| - '}'; |
136 |
| - } |
137 | 96 | }
|
0 commit comments