-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from Qulacs-Osaka/29-add-swap
29 add swap
- Loading branch information
Showing
16 changed files
with
383 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "gate/constant.hpp" | ||
#include "gate/gate.hpp" | ||
#include "gate/gate_npair_qubit.hpp" | ||
#include "gate/gate_one_control_one_target.hpp" | ||
#include "gate/gate_one_qubit.hpp" | ||
#include "gate/gate_quantum_matrix.hpp" | ||
#include "gate/gate_two_qubit.hpp" | ||
#include "gate/update_ops.hpp" | ||
#include "state/state_vector.hpp" | ||
#include "types.hpp" | ||
#include "util/random.hpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "gate_npair_qubit.hpp" | ||
|
||
#include "update_ops.hpp" | ||
|
||
namespace qulacs { | ||
void FusedSWAP::update_quantum_state(StateVector& state_vector) const { | ||
fusedswap_gate(this->qubit_index1, this->qubit_index2, this->block_size, state_vector); | ||
} | ||
} // namespace qulacs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <cassert> | ||
|
||
#include "gate.hpp" | ||
|
||
namespace qulacs { | ||
class FusedSWAP : public QuantumGate { | ||
UINT qubit_index1, qubit_index2, block_size; | ||
|
||
public: | ||
FusedSWAP(UINT _qubit_index1, UINT _qubit_index2, UINT _block_size) | ||
: qubit_index1(_qubit_index1), qubit_index2(_qubit_index2), block_size(_block_size) { | ||
UINT upper_index = std::max(_qubit_index1, _qubit_index2); | ||
UINT lower_index = std::min(_qubit_index1, _qubit_index2); | ||
if (upper_index <= (lower_index + block_size - 1)) { | ||
throw std::runtime_error( | ||
"FusedSwap: upper index must be bigger than lower_index + block_size - 1"); | ||
} | ||
}; | ||
|
||
void update_quantum_state(StateVector& state_vector) const override; | ||
}; | ||
} // namespace qulacs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "gate_two_qubit.hpp" | ||
|
||
#include "update_ops.hpp" | ||
|
||
namespace qulacs { | ||
void SWAP::update_quantum_state(StateVector& state_vector) const { | ||
swap_gate(this->_target1, this->_target2, state_vector); | ||
} | ||
} // namespace qulacs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
#include "gate.hpp" | ||
|
||
namespace qulacs { | ||
class SWAP : public QuantumGate { | ||
UINT _target1, _target2; | ||
|
||
public: | ||
SWAP(UINT target1, UINT target2) : _target1(target1), _target2(target2){}; | ||
void update_quantum_state(StateVector& state_vector) const override; | ||
}; | ||
} // namespace qulacs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <Kokkos_Core.hpp> | ||
#include <Kokkos_StdAlgorithms.hpp> | ||
#include <cassert> | ||
|
||
#include "../types.hpp" | ||
#include "update_ops.hpp" | ||
|
||
namespace qulacs { | ||
void fusedswap_gate(UINT target_qubit_index_0, | ||
UINT target_qubit_index_1, | ||
UINT block_size, | ||
StateVector& state) { | ||
UINT n_qubits = state.n_qubits(); | ||
UINT upper_index, lower_index; | ||
upper_index = std::max(target_qubit_index_0, target_qubit_index_1); | ||
lower_index = std::min(target_qubit_index_0, target_qubit_index_1); | ||
if (n_qubits <= (upper_index + block_size - 1)) { | ||
throw std::runtime_error( | ||
"FusedSwap: num of qubits must be bigger than upper_index + block_size - 1"); | ||
} | ||
const UINT mask_block = (1 << block_size) - 1; | ||
auto amplitudes = state.amplitudes_raw(); | ||
const UINT kblk_mask = mask_block << upper_index; | ||
const UINT jblk_mask = mask_block << lower_index; | ||
const UINT else_mask = (1 << n_qubits) - 1 - kblk_mask - jblk_mask; | ||
|
||
Kokkos::parallel_for( | ||
1 << n_qubits, KOKKOS_LAMBDA(const UINT& i) { | ||
const UINT kblk = (i & kblk_mask) >> upper_index; | ||
const UINT jblk = (i & jblk_mask) >> lower_index; | ||
if (jblk > kblk) { | ||
const UINT index = (i & else_mask) | jblk << upper_index | kblk << lower_index; | ||
Kokkos::Experimental::swap(amplitudes[i], amplitudes[index]); | ||
} | ||
}); | ||
} | ||
} // namespace qulacs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <Kokkos_Core.hpp> | ||
#include <Kokkos_StdAlgorithms.hpp> | ||
|
||
#include "../types.hpp" | ||
#include "constant.hpp" | ||
#include "update_ops.hpp" | ||
|
||
namespace qulacs { | ||
void swap_gate(UINT target0, UINT target1, StateVector& state) { | ||
UINT n_qubits = state.n_qubits(); | ||
UINT mask_0 = 1ULL << target0; | ||
UINT mask_1 = 1ULL << target1; | ||
UINT mask = mask_0 + mask_1; | ||
UINT min_qubit_index = std::min(target0, target1); | ||
UINT max_qubit_index = std::max(target0, target1); | ||
UINT min_qubit_mask = 1ULL << min_qubit_index; | ||
UINT max_qubit_mask = 1ULL << (max_qubit_index - 1); | ||
UINT low_mask = min_qubit_mask - 1; | ||
UINT mid_mask = (max_qubit_mask - 1) ^ low_mask; | ||
UINT high_mask = ~(max_qubit_mask - 1); | ||
auto amplitudes = state.amplitudes_raw(); | ||
Kokkos::parallel_for( | ||
1ULL << (n_qubits - 2), KOKKOS_LAMBDA(const UINT& it) { | ||
UINT basis_index_0 = | ||
(it & low_mask) + ((it & mid_mask) << 1) + ((it & high_mask) << 2) + mask_0; | ||
UINT basis_index_1 = basis_index_0 ^ mask; | ||
Kokkos::Experimental::swap(amplitudes[basis_index_0], amplitudes[basis_index_1]); | ||
}); | ||
} | ||
} // namespace qulacs |
Oops, something went wrong.