Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Single Excitation operations #1121

Merged
merged 67 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 59 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
c211c02
First version of new excitation operations
ixfoduap Feb 24, 2021
c9aa6d4
Adds docstrings + interface operations
ixfoduap Mar 2, 2021
49d231e
Merge branch 'master' into excitations
ixfoduap Mar 2, 2021
143cbe3
Adds support across devices and changelog snippet
ixfoduap Mar 3, 2021
5f12bbc
Adds name to contributors
ixfoduap Mar 3, 2021
6382dc1
Black
ixfoduap Mar 3, 2021
9769875
Update .github/CHANGELOG.md
ixfoduap Mar 3, 2021
0521cd2
small changes
ixfoduap Mar 3, 2021
fa16191
Merge branch 'excitations' of github.com:XanaduAI/pennylane into exci…
ixfoduap Mar 3, 2021
a7c4f97
Merge branch 'master' into excitations
josh146 Mar 4, 2021
ae018d5
Apply suggestions from code review
ixfoduap Mar 4, 2021
23e85ed
Adds tests and improves docstrings
ixfoduap Mar 4, 2021
4cd76e7
Merge branch 'excitations' of github.com:XanaduAI/pennylane into exci…
ixfoduap Mar 4, 2021
882bb77
Update .github/CHANGELOG.md
ixfoduap Mar 4, 2021
40b2320
Update .github/CHANGELOG.md
ixfoduap Mar 4, 2021
3e9f7ba
Apply suggestions from code review
ixfoduap Mar 4, 2021
62683a1
excludes ops from reversible tests
ixfoduap Mar 4, 2021
ac178d3
Merge branch 'excitations' of github.com:XanaduAI/pennylane into exci…
ixfoduap Mar 4, 2021
ced73be
Adds test of ops across interfaces
ixfoduap Mar 9, 2021
6c7bc21
adds docstrings and ignore no-self-use
ixfoduap Mar 10, 2021
50be11c
Merge branch 'master' into excitations
ixfoduap Mar 10, 2021
d8343c2
jax interface for mixed
antalszava Mar 10, 2021
0744aaa
Update pennylane/devices/tests/test_ops.py
ixfoduap Mar 10, 2021
aeb6751
Corrects issues found by checks
ixfoduap Mar 10, 2021
711c72e
Merge branch 'excitations' of github.com:XanaduAI/pennylane into exci…
ixfoduap Mar 10, 2021
e6a00e4
Removes unnecessary import
ixfoduap Mar 10, 2021
95c450c
Removes jax test that is failing
ixfoduap Mar 10, 2021
1b9121d
Update .github/workflows/tests.yml
antalszava Mar 10, 2021
08fbb8b
Update .github/workflows/tests.yml
antalszava Mar 10, 2021
0cc729e
Adds interface tests
ixfoduap Mar 11, 2021
40ffa08
Merge branch 'excitations' of github.com:XanaduAI/pennylane into exci…
ixfoduap Mar 11, 2021
2fedf4c
removes interface tests
ixfoduap Mar 11, 2021
31c05b0
fixs error
ixfoduap Mar 11, 2021
a0bc66a
Adds interface test
ixfoduap Mar 11, 2021
af9f449
removes interface tests
ixfoduap Mar 11, 2021
f141160
adds new interface tests
ixfoduap Mar 15, 2021
6bb3848
Merge branch 'master' into excitations
ixfoduap Mar 16, 2021
2b04db3
adds interface tests for plus and minus excitations
ixfoduap Mar 17, 2021
21d6dc0
Merge branch 'excitations' of github.com:XanaduAI/pennylane into exci…
ixfoduap Mar 17, 2021
c685a6c
adds interface gradient tests
ixfoduap Mar 17, 2021
27adb98
Merge branch 'master' into excitations
ixfoduap Mar 17, 2021
a0b8328
Update tests/ops/test_qubit_ops.py
ixfoduap Mar 18, 2021
be16056
Update tests/ops/test_qubit_ops.py
ixfoduap Mar 18, 2021
5a8432b
Merge branch 'master' into excitations
ixfoduap Mar 18, 2021
a0846e1
Merge branch 'master' into excitations
antalszava Mar 18, 2021
b63561c
Apply suggestions from code review
ixfoduap Mar 18, 2021
0875d71
Apply suggestions from code review
ixfoduap Mar 18, 2021
efb0fc4
adds suggestions from code review
ixfoduap Mar 18, 2021
63a57d3
black
ixfoduap Mar 18, 2021
ea45124
fixes indentation
ixfoduap Mar 18, 2021
5e7eae1
Update .github/CHANGELOG.md
ixfoduap Mar 18, 2021
346edfa
indentation
ixfoduap Mar 18, 2021
b142126
Merge branch 'excitations' of github.com:XanaduAI/pennylane into exci…
ixfoduap Mar 18, 2021
95d9abc
Update .github/CHANGELOG.md
ixfoduap Mar 18, 2021
0c059ea
fixes changelog
ixfoduap Mar 18, 2021
d85a148
this should fix it
ixfoduap Mar 18, 2021
c297f75
Apply suggestions from code review
ixfoduap Mar 19, 2021
434a9ba
fix indentation
ixfoduap Mar 19, 2021
2476d1f
improvements to docstrings
ixfoduap Mar 19, 2021
ac05f79
Merge branch 'master' into excitations
josh146 Mar 22, 2021
9ec2fd3
fix docs
josh146 Mar 22, 2021
9348192
Merge branch 'master' into excitations
ixfoduap Mar 22, 2021
042f2b9
Apply suggestions from code review
ixfoduap Mar 22, 2021
a1ef6f1
Update pennylane/devices/tf_ops.py
ixfoduap Mar 22, 2021
1d5fc6b
changes from code review
ixfoduap Mar 22, 2021
083c61e
Merge branch 'excitations' of github.com:XanaduAI/pennylane into exci…
ixfoduap Mar 22, 2021
ac42e67
fixes tests
ixfoduap Mar 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

<h3>New features since last release</h3>

* Added the `SingleExcitation` two-qubit operation, which is useful for quantum
chemistry applications. [(#1121)](https://github.com/PennyLaneAI/pennylane/pull/1121)

It can be used to perform an SO(2) rotation in the subspace
spanned by the states :math:`|01\rangle` and :math:`|10\rangle`.
For example, the following circuit performs the transformation
:math:`|10\rangle \rightarrow \cos(\phi/2)|10\rangle - \sin(\phi/2)|01\rangle`:

```python
dev = qml.device('default.qubit', wires=2)

@qml.qnode(dev)
def circuit(phi):
qml.PauliX(wires=0)
qml.SingleExcitation(phi, wires=[0, 1])
```

The `SingleExcitation` operation supports analytical gradients on hardware
ixfoduap marked this conversation as resolved.
Show resolved Hide resolved
using only four expectation value calculations, following results from
[Kottmann et al.](https://arxiv.org/abs/2011.05938)

* Added the function ``finite_diff()`` to compute finite-difference
approximations to the gradient and the second-order derivatives of
arbitrary callable functions.
Expand Down Expand Up @@ -371,8 +392,8 @@

This release contains contributions from (in alphabetical order):

Thomas Bromley, Olivia Di Matteo, Kyle Godbey, Diego Guala, Josh Izaac, Daniel Polatajko, Chase Roberts,
Sankalp Sanand, Pritish Sehzpaul, Maria Schuld, Antal Száva.
Juan Miguel Arrazola, Thomas Bromley, Olivia Di Matteo, Kyle Godbey, Diego Guala, Josh Izaac,
Daniel Polatajko, Chase Roberts, Sankalp Sanand, Pritish Sehzpaul, Maria Schuld, Antal Száva.

# Release 0.14.1 (current release)

Expand Down
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"sphinx.ext.intersphinx",
"sphinx_automodapi.automodapi",
'sphinx_copybutton',
"m2r"
# "m2r"
]

source_suffix = ['.rst', '.md']
Expand Down
3 changes: 3 additions & 0 deletions doc/introduction/operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ Qubit gates
~pennylane.MultiControlledX
~pennylane.DiagonalQubitUnitary
~pennylane.QFT
~pennylane.SingleExcitation
~pennylane.SingleExcitationPlus
~pennylane.SingleExcitationMinus

:html:`</div>`

Expand Down
44 changes: 44 additions & 0 deletions pennylane/devices/autograd_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,47 @@ def MultiRZ(theta, n):
array[complex]: diagonal part of the multi-qubit rotation matrix
"""
return np.exp(-1j * theta / 2 * pauli_eigs(n))


def SingleExcitation(phi):
r"""Single excitation rotation.

Args:
phi (float): rotation angle

Returns:
array[float]: Single excitation rotation matrix
"""
c = np.cos(phi / 2)
s = np.sin(phi / 2)
return np.array([[1, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, 1]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring declares that the function returns array[complex] but the matrix elements s and c are real, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!



def SingleExcitationPlus(phi):
r"""Single excitation rotation with positive phase-shift outside the rotation subspace.

Args:
phi (float): rotation angle

Returns:
array[complex]: Single excitation rotation matrix with positive phase-shift
"""
c = np.cos(phi / 2)
s = np.sin(phi / 2)
e = np.exp(1j * phi / 2)
return np.array([[e, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, e]])


def SingleExcitationMinus(phi):
r"""Single excitation rotation with negative phase-shift outside the rotation subspace.

Args:
phi (float): rotation angle

Returns:
array[complex]: Single excitation rotation matrix with negative phase-shift
"""
c = np.cos(phi / 2)
s = np.sin(phi / 2)
e = np.exp(-1j * phi / 2)
return np.array([[e, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, e]])
3 changes: 3 additions & 0 deletions pennylane/devices/default_mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class DefaultMixed(QubitDevice):
"PhaseFlip",
"QubitChannel",
"QFT",
"SingleExcitation",
"SingleExcitationPlus",
"SingleExcitationMinus",
}

def __init__(self, wires, *, shots=1000, analytic=True, cache=0):
Expand Down
3 changes: 3 additions & 0 deletions pennylane/devices/default_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ class DefaultQubit(QubitDevice):
"CRZ",
"CRot",
"QFT",
"SingleExcitation",
"SingleExcitationPlus",
"SingleExcitationMinus",
}

observables = {"PauliX", "PauliY", "PauliZ", "Hadamard", "Hermitian", "Identity"}
Expand Down
3 changes: 3 additions & 0 deletions pennylane/devices/default_qubit_autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class DefaultQubitAutograd(DefaultQubit):
"CRZ": autograd_ops.CRZ,
"CRot": autograd_ops.CRot,
"MultiRZ": autograd_ops.MultiRZ,
"SingleExcitation": autograd_ops.SingleExcitation,
"SingleExcitationPlus": autograd_ops.SingleExcitationPlus,
"SingleExcitationMinus": autograd_ops.SingleExcitationMinus,
}

C_DTYPE = np.complex128
Expand Down
3 changes: 3 additions & 0 deletions pennylane/devices/default_qubit_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ def circuit():
"CRY": jax_ops.CRY,
"CRZ": jax_ops.CRZ,
"MultiRZ": jax_ops.MultiRZ,
"SingleExcitation": jax_ops.SingleExcitation,
"SingleExcitationPlus": jax_ops.SingleExcitationPlus,
"SingleExcitationMinus": jax_ops.SingleExcitationMinus,
}

C_DTYPE = jnp.complex64
Expand Down
3 changes: 3 additions & 0 deletions pennylane/devices/default_qubit_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ class DefaultQubitTF(DefaultQubit):
"CRY": tf_ops.CRY,
"CRZ": tf_ops.CRZ,
"CRot": tf_ops.CRot,
"SingleExcitation": tf_ops.SingleExcitation,
"SingleExcitationPlus": tf_ops.SingleExcitationPlus,
"SingleExcitationMinus": tf_ops.SingleExcitationMinus,
}

C_DTYPE = tf.complex128
Expand Down
44 changes: 44 additions & 0 deletions pennylane/devices/jax_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,47 @@ def MultiRZ(theta, n):
array[complex]: diagonal part of the multi-qubit rotation matrix
"""
return jnp.exp(-1j * theta / 2 * pauli_eigs(n))


def SingleExcitation(phi):
r"""Single excitation rotation.

Args:
phi (float): rotation angle

Returns:
jnp.Tensor[float]: Single excitation rotation matrix
"""
c = jnp.cos(phi / 2)
s = jnp.sin(phi / 2)
return jnp.array([[1, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, 1]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here regarding the type of the returned array, jnp.Tensor[float] ?



def SingleExcitationPlus(phi):
r"""Single excitation rotation with positive phase-shift outside the rotation subspace.

Args:
phi (float): rotation angle

Returns:
jnp.Tensor[complex]: Single excitation rotation matrix with positive phase-shift
"""
c = jnp.cos(phi / 2)
s = jnp.sin(phi / 2)
e = jnp.exp(1j * phi / 2)
return jnp.array([[e, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, e]])


def SingleExcitationMinus(phi):
r"""Single excitation rotation with negative phase-shift outside the rotation subspace.

Args:
phi (float): rotation angle

Returns:
tf.Tensor[complex]: Single excitation rotation matrix with negative phase-shift
"""
c = jnp.cos(phi / 2)
s = jnp.sin(phi / 2)
e = jnp.exp(-1j * phi / 2)
return jnp.array([[e, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, e]])
3 changes: 3 additions & 0 deletions pennylane/devices/tests/test_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
"SX": qml.SX(wires=[0]),
"Toffoli": qml.Toffoli(wires=[0, 1, 2]),
"QFT": qml.QFT(wires=[0, 1, 2]),
"SingleExcitation": qml.SingleExcitation(0, wires=[0, 1]),
"SingleExcitationPlus": qml.SingleExcitationPlus(0, wires=[0, 1]),
"SingleExcitationMinus": qml.SingleExcitationMinus(0, wires=[0, 1]),
}

all_ops = ops.keys()
Expand Down
47 changes: 47 additions & 0 deletions pennylane/devices/tf_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,50 @@ def CRot(a, b, c):
:math:`|0\rangle\langle 0|\otimes \mathbb{I}+|1\rangle\langle 1|\otimes R(a,b,c)`
"""
return tf.linalg.diag(CRZ(c)) @ (CRY(b) @ tf.linalg.diag(CRZ(a)))


def SingleExcitation(phi):
r"""Single excitation rotation.

Args:
phi (float): rotation angle

Returns:
tf.Tensor[float]: Single excitation rotation matrix
ixfoduap marked this conversation as resolved.
Show resolved Hide resolved
"""
phi = tf.cast(phi, dtype=C_DTYPE)
c = tf.cos(phi / 2)
s = tf.sin(phi / 2)
return tf.convert_to_tensor([[1, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, 1]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably the same here: tf.Tensor[float]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: while true for JAX and autograd above, in this case the tensor will be complex, since phi is cast to complex on line 208. So it should remain tf.Tensor[complex].

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: while true for JAX and autograd above, in this case the tensor will be complex, since phi is cast to complex on line 208. So it should remain tf.Tensor[complex].

Attention to details! 😄



def SingleExcitationPlus(phi):
r"""Single excitation rotation with positive phase-shift outside the rotation subspace.

Args:
phi (float): rotation angle

Returns:
tf.Tensor[complex]: Single excitation rotation matrix with positive phase-shift
"""
phi = tf.cast(phi, dtype=C_DTYPE)
c = tf.cos(phi / 2)
s = tf.sin(phi / 2)
e = tf.exp(1j * phi / 2)
return tf.convert_to_tensor([[e, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, e]])


def SingleExcitationMinus(phi):
r"""Single excitation rotation with negative phase-shift outside the rotation subspace.

Args:
phi (float): rotation angle

Returns:
tf.Tensor[complex]: Single excitation rotation matrix with negative phase-shift
"""
phi = tf.cast(phi, dtype=C_DTYPE)
c = tf.cos(phi / 2)
s = tf.sin(phi / 2)
e = tf.exp(-1j * phi / 2)
return tf.convert_to_tensor([[e, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, e]])
Loading