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

Support inverse gates #89

Merged
merged 27 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
3 changes: 2 additions & 1 deletion pennylane_lightning/lightning_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def apply_lightning(self, state, operations):
op_names = [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)
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 std::vector<bool>& inverse,
trbromley marked this conversation as resolved.
Show resolved Hide resolved
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 bools indicating whether a given gate or its inverse should be applied
trbromley marked this conversation as resolved.
Show resolved Hide resolved
* @param qubits total number of qubits
trbromley marked this conversation as resolved.
Show resolved Hide resolved
*/
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