Browse Source

Removed the need for a transpose

Adam Kelly 5 years ago
parent
commit
42b558beb3
3 changed files with 32 additions and 14 deletions
  1. 3 6
      qcgpu/backend.py
  2. 24 3
      qcgpu/state.py
  3. 5 5
      tests/test_gate_application.py

+ 3 - 6
qcgpu/backend.py

@@ -41,7 +41,7 @@ class Backend:
         # Buffer for the state vector
         self.buffer = pycl_array.to_device(
             self.queue,
-            np.eye(2**num_qubits, 1, dtype=dtype)
+            np.eye(1, 2**num_qubits, dtype=dtype)
         )
 
     def apply_gate(self, gate, target):
@@ -49,7 +49,7 @@ class Backend:
 
         self.program.apply_gate(
             self.queue,
-            [int(self.buffer.shape[0] / 2)],
+            [int(self.buffer.shape[1] / 2)],
             None,
             self.buffer.data,
             np.int32(target),
@@ -64,7 +64,7 @@ class Backend:
 
         self.program.apply_controlled_gate(
             self.queue,
-            [int(self.buffer.shape[0] / 2)],
+            [int(self.buffer.shape[1] / 2)],
             None,
             self.buffer.data,
             np.int32(control),
@@ -127,9 +127,6 @@ class Backend:
     def measure_qubit(self, target, samples):
         probability_of_0 = self.qubit_probability(target)
 
-        
-
-
         choices = np.random.choice(
             [0, 1], 
             samples, 

+ 24 - 3
qcgpu/state.py

@@ -97,10 +97,10 @@ class State:
         return self.backend.measure(samples)
 
     def amplitudes(self):
-        return self.backend.amplitudes()
+        return self.backend.amplitudes()[0]
     
     def probabilities(self):
-        return self.backend.probabilities()
+        return self.backend.probabilities()[0]
 
     def flush(self):
         self.backend.release()
@@ -135,7 +135,28 @@ class State:
         self.apply_gate(qcgpu.gate.sqrt_x(), target)
 
     def cx(self, control, target):
-        self.apply_gate(qcgpu.gate.x(), control, target)
+        self.apply_controlled_gate(qcgpu.gate.x(), control, target)
 
     def cnot(self, control, target):
         self.apply_controlled_gate(qcgpu.gate.x(), control, target)
+
+    def u(self, target, theta, phi, lda):
+        a = np.exp(-1j * (phi + lda) / 2) * np.cos(theta / 2)
+        b = - np.exp(-1j * (phi - lda) / 2) * np.sin(theta / 2)
+        c = np.exp(1j * (phi - lda) / 2) * np.sin(theta / 2)    
+        d = np.exp(1j * (phi + lda) / 2) * np.cos(theta / 2)
+    
+        gate_matrix = np.array([
+            [a, b],
+            [c, d]
+        ])
+        self.apply_gate(qcgpu.Gate(gate_matrix), target)
+
+    def u1(self, target, lda):
+        self.u(target, 0, 0, lda)
+
+    def u2(self, target, phi, lda):
+        self.u(target, np.pi / 2, phi, lda)
+
+    def u3(self, target, theta, phi, lda):
+        self.u(target, theta, phi, lda)

+ 5 - 5
tests/test_gate_application.py

@@ -9,7 +9,7 @@ def test_application_x():
     x = qcgpu.gate.x()
     s.apply_gate(x, 0)
 
-    res = np.array([[0,1,0,0,0,0,0,0]]).astype(np.complex64).transpose()
+    res = np.array([0,1,0,0,0,0,0,0]).astype(np.complex64).transpose()
     amps = s.amplitudes()
 
     assert np.allclose(res, amps)
@@ -20,7 +20,7 @@ def test_apply_all_x():
     x = qcgpu.gate.x()
     s.apply_all(x)
 
-    res = np.array([[0,0,0,0,0,0,0,1]]).astype(np.complex64).transpose()
+    res = np.array([0,0,0,0,0,0,0,1]).astype(np.complex64).transpose()
     amps = s.amplitudes()
 
     assert np.allclose(res, amps)
@@ -31,7 +31,7 @@ def test_application_h():
     h = qcgpu.gate.h()
     s.apply_gate(h, 1)
 
-    res = (1/np.sqrt(2)) * np.array([[1,0,1,0,0,0,0,0]]).astype(np.complex64).transpose()
+    res = (1/np.sqrt(2)) * np.array([1,0,1,0,0,0,0,0]).astype(np.complex64).transpose()
     amps = s.amplitudes()
 
     assert np.allclose(res, amps)
@@ -53,7 +53,7 @@ def test_apply_cnot_1():
     x = qcgpu.gate.x()
     s.apply_controlled_gate(x, 0, 1)
 
-    res = np.array([[1,0,0,0]]).astype(np.complex64).transpose()
+    res = np.array([1,0,0,0]).astype(np.complex64).transpose()
     amps = s.amplitudes()
 
     assert np.allclose(res, amps)
@@ -65,7 +65,7 @@ def test_apply_cnot_2():
     s.apply_gate(x, 0)
     s.apply_controlled_gate(x, 0, 1)
 
-    res = np.array([[0,0,0,1]]).astype(np.complex64).transpose()
+    res = np.array([0,0,0,1]).astype(np.complex64).transpose()
     amps = s.amplitudes()
 
     assert np.allclose(res, amps)