Skip to content

Commit d1016ec

Browse files
committedAug 7, 2021
add new setf/shiftf/setp/shiftp command to pulse object
1 parent 0f9250d commit d1016ec

File tree

5 files changed

+54
-23
lines changed

5 files changed

+54
-23
lines changed
 

‎quantum/plugins/ibm/accelerator/IBMAccelerator.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -599,13 +599,10 @@ void IBMAccelerator::execute(
599599
if (qobj_type == "pulse") {
600600
// if we need to use pulse mode, contribute the pulse library of the
601601
// backend (if any)
602-
if (!defaults_response.empty()) {
603-
// Make sure we only contribute pulse cmd-def once
604-
static bool contributed;
605-
if (!contributed) {
606-
contributed = true;
607-
contributeInstructions();
608-
}
602+
// And make sure we only contribute pulse cmd-def once
603+
if (!defaults_response.empty() && !contributed) {
604+
contributed = true;
605+
contributeInstructions();
609606
}
610607
}
611608
// Get the correct QObject Generator
@@ -1090,6 +1087,7 @@ void IBMAccelerator::contributeInstructions(
10901087

10911088
xacc::info("Contributing " + tmpName + " pulse composite.");
10921089
xacc::contributeService(tmpName, cmd_def);
1090+
contributed = true;
10931091
}
10941092
}
10951093

‎quantum/plugins/ibm/accelerator/IBMAccelerator.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class IBMAccelerator : public Accelerator {
195195
std::map<std::string, nlohmann::json> backendProperties;
196196
std::string getBackendPropsResponse = "{}";
197197
std::string defaults_response = "{}";
198+
bool contributed = false;
198199
std::string mode = "qasm";
199200
int requested_n_qubits = 0;
200201
bool filterByJobsLimit = false;

‎quantum/plugins/ibm/accelerator/OpenPulseVisitor.hpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ class OpenPulseVisitor : public BaseInstructionVisitor,
9090
inst.set_phase(i.getParameter(0).as<double>());
9191
inst.set_t0(i.start());
9292

93-
std::vector<std::string> builtIns {"fc", "acquire", "parametric_pulse", "delay" };
93+
std::vector<std::string> builtIns {"fc", "acquire", "parametric_pulse", "delay",
94+
"setf","shiftf", "setp", "shiftp" };
9495
if (std::find(builtIns.begin(), builtIns.end(), i.name()) == std::end(builtIns)) {
9596
// add to default libr
9697
xacc::ibm_pulse::PulseLibrary lib;
@@ -122,6 +123,10 @@ class OpenPulseVisitor : public BaseInstructionVisitor,
122123
inst.set_pulse_params(pulseParameters.getString("parameters_json"));
123124
}
124125

126+
if (i.name() == "setf" || i.name() == "shiftf") {
127+
inst.set_frequency(i.getParameter(0).as<double>());
128+
}
129+
125130
instructions.push_back(inst);
126131

127132
}

‎quantum/plugins/ibm/accelerator/json/PulseQObject.hpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class Instruction {
195195
std::string name;
196196
int64_t t0;
197197
double phase;
198+
double frequency;
198199
std::vector<int64_t> memory_slot;
199200
int64_t duration;
200201
std::vector<int64_t> qubits;
@@ -215,6 +216,9 @@ class Instruction {
215216
double get_phase() const { return phase; }
216217
void set_phase(double value) { this->phase = value; }
217218

219+
double get_frequency() const { return frequency; }
220+
void set_frequency(double value) { this->frequency = value; }
221+
218222
std::vector<int64_t> get_memory_slot() const { return memory_slot; }
219223
void set_memory_slot(std::vector<int64_t> value) {
220224
this->memory_slot = value;
@@ -459,7 +463,7 @@ inline void to_json(json &j, const xacc::ibm_pulse::Instruction &x) {
459463

460464
j["name"] = x.get_name();
461465
j["t0"] = x.get_t0();
462-
if (x.get_name() == "fc") {
466+
if (x.get_name() == "fc" || x.get_name() == "setp" || x.get_name() == "shiftp") {
463467
j["phase"] = x.get_phase();
464468
}
465469
if (x.get_name() == "acquire") {
@@ -477,6 +481,9 @@ inline void to_json(json &j, const xacc::ibm_pulse::Instruction &x) {
477481
if (x.get_name() == "delay") {
478482
j["duration"] = x.get_duration();
479483
}
484+
if (x.get_name() == "setf" || x.get_name() == "shiftf") {
485+
j["frequency"] = x.get_frequency();
486+
}
480487
}
481488

482489
inline void from_json(const json &j, xacc::ibm_pulse::Experiment &x) {

‎quantum/plugins/ibm/tests/IBMPulseRemoteTester.cpp

+34-14
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,12 @@ TEST(IBMPulseRemoteTester, checkCnotPulse) {
126126
}
127127
#endif
128128

129-
// Check manipulations with frequency
130-
TEST(IBMPulseRemoteTester, checkFrequencyHandle) {
129+
// Check manipulations with phase and frequency
130+
TEST(IBMPulseRemoteTester, sendPhaseFrequencyPulse) {
131131
xacc::set_verbose(true);
132132
auto acc = xacc::getAccelerator("ibm:ibmq_armonk", {{"mode", "pulse"}});
133+
auto buffer = xacc::qalloc(1);
134+
auto provider = xacc::getService<xacc::IRProvider>("quantum");
133135
std::string jjson("{"
134136
"\"pulse_library\": ["
135137
"{\"name\": \"pulse1\", \"samples\": [[0,0],[0,0],[0,0]]},"
@@ -138,32 +140,50 @@ TEST(IBMPulseRemoteTester, checkFrequencyHandle) {
138140
"]"
139141
","
140142
"\"cmd_def\":["
141-
"{\"name\":\"test_freq\",\"qubits\":[0],\"sequence\":["
142-
"{\"name\":\"setf\",\"ch\":\"d0\",\"t0\":0,\"frequency\":5}"
143-
","
144-
"{\"name\":\"shiftf\",\"ch\":\"d0\",\"t0\":0,\"frequency\":-0.1}"
145-
","
146-
"{\"name\":\"setp\",\"ch\":\"d0\",\"t0\":0,\"phase\":-1.57}"
143+
"{\"name\":\"test_phase\",\"qubits\":[0],\"sequence\":["
144+
"{\"name\":\"setp\",\"ch\":\"d0\",\"t0\":0,\"phase\":1.57}"
147145
","
148146
"{\"name\":\"shiftp\",\"ch\":\"d0\",\"t0\":0,\"phase\":0.1}"
149147
"]}"
150148
","
149+
"{\"name\":\"test_freq\",\"qubits\":[0],\"sequence\":["
150+
"{\"name\":\"setf\",\"ch\":\"d0\",\"t0\":0,\"frequency\":5.1}"
151+
","
152+
"{\"name\":\"shiftf\",\"ch\":\"d0\",\"t0\":0,\"frequency\":-0.21}"
153+
"]}"
154+
","
151155
"{\"name\":\"id2\",\"qubits\":[0],\"sequence\":[{\"name\":\"setf\",\"ch\":\"d0\",\"t0\":0,\"frequency\":5}]}"
152156
"] "
153157
"}");
154158

155159
acc->contributeInstructions(jjson);
156-
auto cr = xacc::getContributedService<xacc::Instruction>("pulse::test_freq_0");
157-
auto cr_comp =
158-
std::dynamic_pointer_cast<xacc::CompositeInstruction>(cr);
160+
{
161+
// ibmq_armonk does not support setp/shiftp pulse operations.
162+
// test only contributeInstructions()
163+
auto cr = xacc::getContributedService<xacc::Instruction>("pulse::test_phase_0");
164+
auto cr_comp = std::dynamic_pointer_cast<xacc::CompositeInstruction>(cr);
159165

160-
EXPECT_EQ(cr_comp->getInstructions().size(), 4);
166+
EXPECT_EQ(cr_comp->getInstructions().size(), 2);
167+
std::string checkNames[] = {"setp", "shiftp"};
168+
for( int nI = 0; nI < 2; ++nI ) {
169+
EXPECT_EQ(cr_comp->getInstruction(nI)->name(), checkNames[nI] );
170+
}
171+
}
161172

162-
std::string checkNames[] = {"setf", "shiftf", "setp", "shiftp"};
173+
auto cr = xacc::getContributedService<xacc::Instruction>("pulse::test_freq_0");
174+
auto cr_comp = std::dynamic_pointer_cast<xacc::CompositeInstruction>(cr);
163175

164-
for( int nI = 0; nI < 4; ++nI ) {
176+
EXPECT_EQ(cr_comp->getInstructions().size(), 2);
177+
std::string checkNames[] = {"setf", "shiftf"};
178+
for( int nI = 0; nI < 2; ++nI ) {
165179
EXPECT_EQ(cr_comp->getInstruction(nI)->name(), checkNames[nI] );
166180
}
181+
182+
auto f = provider->createComposite("tmp");
183+
auto m0 = provider->createInstruction("Measure", {0});
184+
f->addInstructions({ cr_comp, m0 });
185+
acc->execute(buffer, f);
186+
buffer->print();
167187
}
168188

169189
int main(int argc, char **argv) {

0 commit comments

Comments
 (0)