Skip to content

Commit

Permalink
Merge 006ff7f into fd41d8f
Browse files Browse the repository at this point in the history
  • Loading branch information
trbromley authored Mar 22, 2021
2 parents fd41d8f + 006ff7f commit 6e59a18
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 58 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

### Improvements

* Inverse gates are now supported.
[(#89)](https://github.com/PennyLaneAI/pennylane-lightning/pull/89)

* Add new lightweight backend with performance improvements.
[(#57)](https://github.com/PennyLaneAI/pennylane-lightning/pull/57)

Expand Down
19 changes: 16 additions & 3 deletions pennylane_lightning/lightning_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def capabilities(cls):
capabilities.update(
model="qubit",
supports_reversible_diff=False,
supports_inverse_operations=False,
supports_inverse_operations=True,
supports_analytic_computation=True,
returns_state=True,
)
Expand Down Expand Up @@ -133,10 +133,23 @@ def apply_lightning(self, state, operations):
Returns:
array[complex]: the output state tensor
"""
op_names = [o.name for o in operations]
op_names = [self._remove_inverse_string(o.name) for o in operations]
op_wires = [self.wires.indices(o.wires) for o in operations]
op_param = [o.parameters for o in operations]
op_inverse = [o.inverse for o in operations]

state_vector = np.ravel(state)
apply(state_vector, op_names, op_wires, op_param, self.num_wires)
apply(state_vector, op_names, op_wires, op_param, op_inverse, self.num_wires)
return np.reshape(state_vector, state.shape)

@staticmethod
def _remove_inverse_string(string):
"""Removes the ``.inv`` appended to the end of inverse gates.
Args:
string (str): name of operation
Returns:
str: name of operation with ``.inv`` removed (if present)
"""
return string.replace(".inv", "")
6 changes: 4 additions & 2 deletions pennylane_lightning/src/Apply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void Pennylane::constructAndApplyOperation(
const string& opLabel,
const vector<unsigned int>& opWires,
const vector<double>& opParams,
bool inverse,
const unsigned int qubits
) {
unique_ptr<AbstractGate> gate = constructGate(opLabel, opParams);
Expand All @@ -64,14 +65,15 @@ void Pennylane::constructAndApplyOperation(
vector<unsigned int> externalWires = getIndicesAfterExclusion(opWires, qubits);
vector<size_t> externalIndices = generateBitPatterns(externalWires, qubits);

gate->applyKernel(state, internalIndices, externalIndices);
gate->applyKernel(state, internalIndices, externalIndices, inverse);
}

void Pennylane::apply(
StateVector& state,
const vector<string>& ops,
const vector<vector<unsigned int>>& wires,
const vector<vector<double>>& params,
const vector<bool>& inverse,
const unsigned int qubits
) {
if (qubits <= 0)
Expand All @@ -86,7 +88,7 @@ void Pennylane::apply(
throw std::invalid_argument("Invalid arguments: number of operations, wires, and parameters must all be equal");

for (int i = 0; i < numOperations; i++) {
constructAndApplyOperation(state, ops[i], wires[i], params[i], qubits);
constructAndApplyOperation(state, ops[i], wires[i], params[i], inverse[i], qubits);
}

}
5 changes: 5 additions & 0 deletions pennylane_lightning/src/Apply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ namespace Pennylane {
* @param opLabel unique string corresponding to a gate type
* @param opWires index of qubits on which the gate acts
* @param opParams defines the gate parameterisation (may be zero-length for some gates)
* @param inverse boolean indicating whether to apply the gate or its inverse
* @param qubits number of qubits
*/
void constructAndApplyOperation(
StateVector& state,
const std::string& opLabel,
const std::vector<unsigned int>& opWires,
const std::vector<double>& opParams,
bool inverse,
const unsigned int qubits
);

Expand All @@ -79,12 +81,15 @@ namespace Pennylane {
* @param ops list of unique string names corresponding to gate types, in the order they should be applied
* @param wires list of wires on which each gate acts
* @param params list of parameters that defines the gate parameterisation
* @param inverse list of booleans indicating whether a given gate or its inverse should be applied
* @param qubits number of qubits
*/
void apply(
StateVector& state,
const std::vector<std::string>& ops,
const std::vector<std::vector<unsigned int>>& wires,
const std::vector<std::vector<double>>& params,
const std::vector<bool>& inverse,
const unsigned int qubits
);

Expand Down
3 changes: 2 additions & 1 deletion pennylane_lightning/src/Bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ void apply(
vector<string> ops,
vector<vector<unsigned int>> wires,
vector<vector<double>> params,
vector<bool> inverse,
const unsigned int qubits
) {
StateVector state = create(&stateNumpyArray);
Pennylane::apply(state, ops, wires, params, qubits);
Pennylane::apply(state, ops, wires, params, inverse, qubits);
}

PYBIND11_MODULE(lightning_qubit_ops, m)
Expand Down
Loading

0 comments on commit 6e59a18

Please sign in to comment.