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 any two qubit gates #235

Draft
wants to merge 32 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c845d37
🔥 Remove assert in countGates
JoachimMarin Feb 13, 2023
d526398
✨ Preserve operation type for two qubit gates
JoachimMarin Feb 13, 2023
27b1b20
🚧 Generalize naming for two-qubit gates
JoachimMarin Feb 13, 2023
7002c93
🎨 Rename cnots to twoQubitGates
JoachimMarin Feb 13, 2023
03f4214
🎨 pre-commit fixes
pre-commit-ci[bot] Feb 13, 2023
5f49023
:sparkles: Case distinction for flipping gates
JoachimMarin Feb 20, 2023
4e8648a
🎨 pre-commit fixes
pre-commit-ci[bot] Feb 20, 2023
6d33c87
✨ Function for gate reversal cost
JoachimMarin Mar 4, 2023
3b05b62
:rewind: Assume cnot gates in distance table
JoachimMarin Mar 5, 2023
7b38085
:white_check_mark: Add direction reverse python tests
JoachimMarin Mar 5, 2023
e7127dd
:bulb: Add python test descriptions
JoachimMarin Mar 6, 2023
6dfbb94
🚨 Fix ruff warnings for python tests
JoachimMarin Mar 6, 2023
4016c86
Merge remote-tracking branch 'upstream/main' into support-any-two-qub…
JoachimMarin Mar 6, 2023
ff3929c
🚨 Fix missing return on unexpected enum value
JoachimMarin Mar 6, 2023
91b7969
✅ Remove InstructionSet import
JoachimMarin Mar 6, 2023
219eda2
✅ Use gate objects for python tests
JoachimMarin Mar 7, 2023
2bd795e
🩹 fix Python typing
burgholzer Mar 7, 2023
88a82ee
🩹 silence an external Qiskit warning from the tests
burgholzer Mar 7, 2023
90764c3
Merge remote-tracking branch 'origin/main' into support-any-two-qubit…
burgholzer Mar 7, 2023
f0f19e3
♻️ restructure tests
burgholzer Mar 7, 2023
e71fb87
🐛 Prevent reversals with swaps in exact mapper
JoachimMarin Mar 11, 2023
45deaab
Update include/Mapper.hpp
burgholzer Mar 11, 2023
e43c357
🚨 some `const`
burgholzer Mar 11, 2023
3c982bc
🚚 rename `insertFlippedGate` method
burgholzer Mar 11, 2023
80609be
♻️ refactor condition to simplify code
burgholzer Mar 11, 2023
44d81c8
⚡ add missing conditional skip statement
burgholzer Mar 11, 2023
31e73b3
🚸 add additional sanity check
burgholzer Mar 11, 2023
a33e1d1
🚸 add `const` version of the mapped circuit getter
burgholzer Mar 11, 2023
12ab7fa
♻️ simplify tests
burgholzer Mar 11, 2023
0aec519
🐛 fix coupling limit computation for directed architectures
burgholzer Mar 11, 2023
8897aa4
🧪 Add direction reverse tests for heuristic mapper
JoachimMarin Mar 14, 2023
34cc4bd
✨ Update heuristic algorithm for direction reverse
JoachimMarin Mar 14, 2023
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
24 changes: 22 additions & 2 deletions include/Mapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,33 @@ class Mapper {
countGates(circuit.cbegin(), circuit.cend(), info);
}
/**
* @brief count number of elementary gates and cnots in circuit and save the
* results in `info.gates` and `info.cnots`
* @brief count number of elementary gates and controlled gates in circuit and
burgholzer marked this conversation as resolved.
Show resolved Hide resolved
* save the results in `info.gates` and `info.twoQubitGates`
*/
virtual void countGates(decltype(qcMapped.cbegin()) it,
const decltype(qcMapped.cend())& end,
MappingResults::CircuitInfo& info);

/**
* @brief inserts a two qubit operation op along edge.
* @param edge the edge in the coupling graph that controls on which physical
* qubits the operation will act. The logical and physical direction are the
* same.
* @param op the operation which is inserted
* @return total number of inserted operations
*/
std::size_t insertGate(const Edge& edge, qc::StandardOperation* op);

/**
* @brief inserts a two qubit operation op along a direction reversed edge.
* @param edge the edge in the coupling graph that controls on which physical
* qubits the operation will act. The logical direction of the operation is
* the reverse of the physical direction.
* @param op the operation which is inserted
* @return total number of inserted operations
*/
std::size_t insertFlippedGate(const Edge& edge, qc::StandardOperation* op);
burgholzer marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief performs optimizations on the circuit before mapping
*
Expand Down
41 changes: 41 additions & 0 deletions src/Mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,44 @@ void Mapper::countGates(decltype(qcMapped.cbegin()) it,
}
}
}
std::size_t Mapper::insertGate(const Edge& edge, qc::StandardOperation* op) {
qcMapped.emplace_back<qc::StandardOperation>(
qcMapped.getNqubits(), qc::Control{static_cast<qc::Qubit>(edge.first)},
edge.second, op->getType(), op->getParameter().at(0),
op->getParameter().at(1), op->getParameter().at(2));
burgholzer marked this conversation as resolved.
Show resolved Hide resolved
return 1;
}

std::size_t Mapper::insertFlippedGate(const Edge& edge, qc::StandardOperation* op) {
int swapType = -1;
switch (op->getType()) {
case qc::X:
burgholzer marked this conversation as resolved.
Show resolved Hide resolved
swapType = 1;
break;
case qc::Z:
burgholzer marked this conversation as resolved.
Show resolved Hide resolved
swapType = 0;
break;
default:
swapType = -1;
}
burgholzer marked this conversation as resolved.
Show resolved Hide resolved

if(swapType == 0) {
// no additional gates required to reverse direction
return insertGate(edge, op);
}
if(swapType == 1) {
// four hadamard gates to reverse direction
qcMapped.h(edge.first);
qcMapped.h(edge.second);
auto tmp = insertGate(edge, op);
qcMapped.h(edge.first);
qcMapped.h(edge.second);
return tmp + 4;
}

// in all other cases, swap gates are used to reverse direction
qcMapped.swap(edge.first, edge.second);
auto tmp = insertGate(edge, op);
qcMapped.swap(edge.second, edge.first);
burgholzer marked this conversation as resolved.
Show resolved Hide resolved
return 2 + tmp;
}
Fixed Show fixed Hide fixed
16 changes: 2 additions & 14 deletions src/exact/ExactMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,26 +294,14 @@ void ExactMapper::map(const Configuration& settings) {
<< " with control and target: " << controlledGate.first
<< " " << controlledGate.second << std::endl;
}
qcMapped.h(reverse.first);
qcMapped.h(reverse.second);
qcMapped.emplace_back<qc::StandardOperation>(
qcMapped.getNqubits(),
qc::Control{static_cast<qc::Qubit>(reverse.first)},
reverse.second, op->getType(), op->getParameter().at(0),
op->getParameter().at(1), op->getParameter().at(2));
qcMapped.h(reverse.second);
qcMapped.h(reverse.first);
insertFlippedGate(reverse, op);
} else {
if (settings.verbose) {
std::cout << i << ": Added controlled gate " << op->getName()
<< " with control and target: " << controlledGate.first
<< " " << controlledGate.second << std::endl;
}
qcMapped.emplace_back<qc::StandardOperation>(
qcMapped.getNqubits(),
qc::Control{static_cast<qc::Qubit>(controlledGate.first)},
controlledGate.second, op->getType(), op->getParameter().at(0),
op->getParameter().at(1), op->getParameter().at(2));
insertGate(controlledGate, op);
}
}
}
Expand Down
19 changes: 2 additions & 17 deletions src/heuristic/HeuristicMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,10 @@ void HeuristicMapper::map(const Configuration& configuration) {
": " + std::to_string(reverse.first) + "-" +
std::to_string(reverse.second));
}
qcMapped.h(reverse.first);
qcMapped.h(reverse.second);
qcMapped.emplace_back<qc::StandardOperation>(
qcMapped.getNqubits(),
qc::Control{static_cast<qc::Qubit>(reverse.first)},
reverse.second, op->getType(), op->getParameter().at(0),
op->getParameter().at(1), op->getParameter().at(2));
qcMapped.h(reverse.second);
qcMapped.h(reverse.first);

gateidx += insertFlippedGate(reverse, op);
results.output.directionReverse++;
gateidx += 5;
} else {
qcMapped.emplace_back<qc::StandardOperation>(
qcMapped.getNqubits(),
qc::Control{static_cast<qc::Qubit>(controlledGate.first)},
controlledGate.second, op->getType(), op->getParameter().at(0),
op->getParameter().at(1), op->getParameter().at(2));
gateidx++;
gateidx += insertGate(controlledGate, op);
}
}
}
Expand Down