Skip to content

Commit 8d27964

Browse files
author
Stephen DiAdamo
committed
Adds needed 2 qubit control gate wrapper
1 parent 2c0ac25 commit 8d27964

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

qunetsim/backends/cqc_backend.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def custom_gate(self, qubit, gate):
339339
qubit(Qubit): Qubit to which the gate is applied.
340340
gate(np.ndarray): 2x2 array of the gate.
341341
"""
342-
raise(EnvironmentError("Not implemented for this backend!"))
342+
raise (EnvironmentError("Not implemented for this backend!"))
343343

344344
def custom_controlled_gate(self, qubit, target, gate):
345345
"""
@@ -350,7 +350,7 @@ def custom_controlled_gate(self, qubit, target, gate):
350350
target(Qubit): Qubit on which the gate is applied.
351351
gate(nd.array): 2x2 array for the gate applied to target.
352352
"""
353-
raise(EnvironmentError("Not implemented for this backend!"))
353+
raise (EnvironmentError("Not implemented for this backend!"))
354354

355355
def custom_two_qubit_gate(self, qubit1, qubit2, gate):
356356
"""
@@ -361,7 +361,19 @@ def custom_two_qubit_gate(self, qubit1, qubit2, gate):
361361
qubit2(Qubit): Second qubit of the gate.
362362
gate(np.ndarray): 4x4 array for the gate applied.
363363
"""
364-
raise(EnvironmentError("Not implemented for this backend!"))
364+
raise (EnvironmentError("Not implemented for this backend!"))
365+
366+
def custom_controlled_two_qubit_gate(self, qubit, target_1, target_2, gate):
367+
"""
368+
Applies a custom gate to the target qubit, controlled by the qubit.
369+
370+
Args:
371+
qubit (Qubit): Qubit to control the gate.
372+
target_1 (Qubit): Qubit on which the gate is applied.
373+
target_2 (Qubit): Qubit on which the gate is applied.
374+
gate (nd.array): 4x4 array for the gate applied to target.
375+
"""
376+
raise (EnvironmentError("Not implemented for this backend!"))
365377

366378
def measure(self, qubit, non_destructive):
367379
"""

qunetsim/backends/eqsn_backend.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,18 @@ def custom_controlled_gate(self, qubit, target, gate):
373373
"""
374374
self.eqsn.custom_controlled_gate(target.qubit, qubit.qubit, gate)
375375

376+
def custom_controlled_two_qubit_gate(self, qubit, target_1, target_2, gate):
377+
"""
378+
Applies a custom gate to the target qubit, controlled by the qubit.
379+
380+
Args:
381+
qubit (Qubit): Qubit to control the gate.
382+
target_1 (Qubit): Qubit on which the gate is applied.
383+
target_2 (Qubit): Qubit on which the gate is applied.
384+
gate (nd.array): 4x4 array for the gate applied to target.
385+
"""
386+
self.eqsn.custom_two_qubit_control_gate(qubit.qubit, target_1.qubit, target_2.qubit, gate)
387+
376388
def custom_two_qubit_gate(self, qubit1, qubit2, gate):
377389
"""
378390
Applies a custom two qubit gate to qubit1 \\otimes qubit2.

qunetsim/backends/projectq_backend.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def custom_gate(self, qubit, gate):
288288
qubit(Qubit): Qubit to which the gate is applied.
289289
gate(np.ndarray): 2x2 array of the gate.
290290
"""
291-
raise(EnvironmentError("Not implemented for this backend!"))
291+
raise (EnvironmentError("Not implemented for this backend!"))
292292

293293
def custom_controlled_gate(self, qubit, target, gate):
294294
"""
@@ -299,7 +299,7 @@ def custom_controlled_gate(self, qubit, target, gate):
299299
target(Qubit): Qubit on which the gate is applied.
300300
gate(nd.array): 2x2 array for the gate applied to target.
301301
"""
302-
raise(EnvironmentError("Not implemented for this backend!"))
302+
raise (EnvironmentError("Not implemented for this backend!"))
303303

304304
def custom_two_qubit_gate(self, qubit1, qubit2, gate):
305305
"""
@@ -310,7 +310,19 @@ def custom_two_qubit_gate(self, qubit1, qubit2, gate):
310310
qubit2(Qubit): Second qubit of the gate.
311311
gate(np.ndarray): 4x4 array for the gate applied.
312312
"""
313-
raise(EnvironmentError("Not implemented for this backend!"))
313+
raise (EnvironmentError("Not implemented for this backend!"))
314+
315+
def custom_controlled_two_qubit_gate(self, qubit, target_1, target_2, gate):
316+
"""
317+
Applies a custom gate to the target qubit, controlled by the qubit.
318+
319+
Args:
320+
qubit (Qubit): Qubit to control the gate.
321+
target_1 (Qubit): Qubit on which the gate is applied.
322+
target_2 (Qubit): Qubit on which the gate is applied.
323+
gate (nd.array): 4x4 array for the gate applied to target.
324+
"""
325+
raise (EnvironmentError("Not implemented for this backend!"))
314326

315327
def measure(self, qubit, non_destructive):
316328
"""

qunetsim/objects/qubit.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,38 @@ def custom_controlled_gate(self, target, gate):
241241

242242
self._host.backend.custom_controlled_gate(self, target, gate)
243243

244+
def custom_two_qubit_control_gate(self, q1, q2, gate):
245+
"""
246+
Applies the gate *gate* to qubits q1 and q2 as a controlled gate.
247+
248+
Args:
249+
q1 (Qubit): First qubit
250+
q2 (Qubit): Second qubit
251+
gate (Numpy ndarray): The gate to apply
252+
"""
253+
if not isinstance(gate, np.ndarray):
254+
raise (InputError("Only Numpy matrices are allowed"))
255+
if not is_unitary(gate):
256+
raise (InputError("Custom gates must be unitary operations"))
257+
if gate.shape != (4, 4):
258+
raise (InputError("Custom controlled gates must be 4x4 matrices"))
259+
260+
self._host.backend.custom_controlled_two_qubit_gate(self, q1, q2, gate)
261+
244262
def custom_two_qubit_gate(self, other_qubit, gate):
245263
"""
246264
Applies a custom 2 qubit gate.
247265
248266
Args:
249267
other_qubit (Qubit): The second qubit.
250268
gate (Numpy ndarray): The gate
251-
252269
"""
253270
if not isinstance(gate, np.ndarray):
254271
raise (InputError("Only Numpy matrices are allowed"))
255272
if not is_unitary(gate):
256273
raise (InputError("Custom gates must be unitary operations"))
257274
if gate.shape != (4, 4):
258-
raise (InputError("Custom controlled gates must be 2x2 matrices"))
275+
raise (InputError("Custom controlled gates must be 4x4 matrices"))
259276

260277
self._host.backend.custom_two_qubit_gate(self, other_qubit, gate)
261278

0 commit comments

Comments
 (0)