Skip to content

Commit

Permalink
add conditional sum gate
Browse files Browse the repository at this point in the history
  • Loading branch information
tjstavenger-pnnl committed Dec 4, 2024
1 parent f2b7476 commit a284f87
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
28 changes: 28 additions & 0 deletions c2qa/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,34 @@ def cv_sum(self, scale, qumode_a, qumode_b, duration=100, unit="ns"):
qargs=qumode_a + qumode_b,
)

def cv_c_sum(self, scale, qumode_a, qumode_b, qubit, duration=100, unit="ns"):
"""Conditional two-mode sum gate.
Args:
scale (real): arbitrary real scale factor
qumode_a (list): list of qubits representing qumode
qumode_b (list): list of qubits representing qumode
qubit (Qubit): control Qubit
Returns:
Instruction: QisKit instruction
"""
return self.append(
ParameterizedUnitaryGate(
self.ops.csum,
[scale],
cutoffs=[
QumodeRegister.calculate_cutoff(len(qumode_a)),
QumodeRegister.calculate_cutoff(len(qumode_b)),
],
num_qubits=len(qumode_a) + len(qumode_b) + 1,
label="cSum",
duration=duration,
unit=unit,
),
qargs=qumode_a + qumode_b + [qubit],
)

def measure_z(self, qubit, cbit, duration=100, unit="ns"):
"""Measure qubit in z using probe qubits
Expand Down
20 changes: 20 additions & 0 deletions c2qa/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,23 @@ def sum(self, scale, cutoff_a, cutoff_b):
arg = (scale / 2) * (scipy.sparse.kron(a_mat, b_mat))

return scipy.sparse.linalg.expm(arg)

def csum(self, scale, cutoff_a, cutoff_b):
"""Conditional two-qumode sum gate
Args:
scale (real): arbitrary scale factor
Returns:
csc_matrix: operator matrix
"""
# TODO verify below implementation
# equation 205 from https://arxiv.org/pdf/2407.10381
# vs equation 4 from https://journals.aps.org/prl/pdf/10.1103/PhysRevLett.88.097904
# Equation 205 with matrix multiplication is not unitary, how to handle different cutoffs
a_mat = self.get_a(cutoff_a) + self.get_a_dag(cutoff_a)
b_mat = self.get_a_dag(cutoff_b) - self.get_a(cutoff_b)
arg = (scale / 2) * (scipy.sparse.kron(a_mat, b_mat))
arg = scipy.sparse.kron(zQB, arg)

return scipy.sparse.linalg.expm(arg)
10 changes: 10 additions & 0 deletions tests/test_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,16 @@ def test_two_mode_sum():
assert_changed(state, result)


def test_conditional_two_mode_sum():
circuit, qmr, qbr = create_conditional()

z = random.random()
circuit.cv_c_sum(z, qmr[0], qmr[1], qbr[0])

state, result, fock_counts = c2qa.util.simulate(circuit)
assert_changed(state, result)


def random_real_and_complex():
return [
random.random(),
Expand Down
20 changes: 15 additions & 5 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ def test_s(self):

def test_s2(self):
assert is_unitary_matrix(self.ops.s2(random.random(), self.cutoff, self.cutoff))

def test_sum(self):
mat = self.ops.sum(random.random(), self.cutoff, self.cutoff)
print(mat.toarray())
assert is_unitary_matrix(mat)
assert is_unitary_matrix(
self.ops.sum(random.random(), self.cutoff, self.cutoff)
)

def test_csum(self):
assert is_unitary_matrix(
self.ops.csum(random.random(), self.cutoff, self.cutoff)
)


class TestMatrices:
Expand Down Expand Up @@ -139,4 +143,10 @@ def test_sum(self):
one = self.ops.sum(1, self.cutoff, self.cutoff)
rand = self.ops.sum(random.random(), self.cutoff, self.cutoff)

assert not allclose(one, rand)
assert not allclose(one, rand)

def test_csum(self):
one = self.ops.csum(1, self.cutoff, self.cutoff)
rand = self.ops.csum(random.random(), self.cutoff, self.cutoff)

assert not allclose(one, rand)

0 comments on commit a284f87

Please sign in to comment.